Sunday, 28 August 2016

Git Commands (Part-1):





Git is a powerful that is used for software and other version control tasks, but you can feel its futures only when you’re using its commands. Git commands are more flexible than any other tools. You can make any folder as git repository, to track the changes. As I said earlier it will creates ‘.git’ hidden folder to track the changes.

Let’s dive in to it...............

How to create/initialize git:

Note: GitShell accept most of Unix and dos commands

Go to your folder which you would like to initialize git, then enter ‘git init’. It will create an empty repository for you.

How to add content to your git:

Before adding content you can see what is available to add by using ‘git status’:

We have one branch master with Initial Commit and Demo_Code.scala files which needs to be committed.

Now I know what are files are available to add. I am adding it using ‘git add .’. Here I am using ‘.’ since all the files in this directory needs to be added to the directory. In case you’re looking to add only one file addition you can use ‘git add file_name’.


Note: If you’re getting warning message like ‘LF will be replaced by CRLF’. Please ignore it. It is nothing to do with your code. When you copy file from unix to windows the end of line represented as LF will be converted to CRLF. 



If you don’t want to see this manage again and again you can set autocrlf on in your config file with ‘git config core.autocrlf off’

Once you execute git add command, files are added to index area. It has different names to it, some of them are: cached, Staging area, Current directory cache. It’s not the git repository, Index is like a staging area to commit your changes. 

                                                                    Simple Git Architecture look like this

Changes are added to Index. Now I am committing changes to repository using ‘git commit –m “your commit comments”’.

All my files are added to my local repository. 


We can use ‘git commit - am “provide your comments”’ as shortcut to add your files to index and commit changes to git repository in one command. 


How to check your changes in repository:
With our previous commit we have also provided comments. It is very important to provide a valid and meaningful comments about your changes. We will use these same comments to identify commits and for every commits git will generate SHA-1 which we have already discussed in my previous post. We can see all the commits using ‘git log’.
Your commits can be identified using:
  • 1     Date and time of commits
  • 2.       Your comments
  • 3.       All your commits are arranged from recent commits to old commits. It means your most recent commit will be on top and follows all the rest old commits. 
  • 4.       Who made the commits and email address.

We can get short log using: using ‘git shortlog’


We can see most recent commit:  using ‘git log’



If we would like to see ‘N’ number of recent commits: we can see it using ‘git log –n 3’:


If we would like to see all the details in one line: with SHA and commit comments we can see it using ‘git log --oneline’


Note: Commit comments essential when we are trying to analyse our previous commits. In the above screen shot if we look at the second commit, commit comments are vogue it will not give you which file has been removed. It is recommended to provide good and short comments.

If we would like to decorate the log we can use: ‘git log --oneline --decorate’ . Here ‘decorate provide details about your branch and tag information. Which will explore it next blog.


If we would like to see the change summary and statistics: we can use ‘git log --stat --summary’


Note: statistics will provide details about what has been added with ‘+’ and what has been deleted with ‘-‘.

We can filter logs using author name: by using ‘git log --author=“Niranjan”’

We can pass email address to author to filter log: by using ‘git log --author=”b.niranjanrao@gmail.com”’


We can filter commits using global regular expressions as: by using ‘git log –grep=”Init”’


We can see log based on file: by using ‘git log file_name’


How to find differences between your working directory and git: 
  • Differences between git and working repository using ‘git diff 
  • Differences between git index and working repository using ‘git diff --cached’

How to remove Indexed/Cached file:

We can remove file from Indexed/Cached using ‘git rm –cache file_name’
At a time we can remove more than one file:

We will continue exploring the rest of the git commands in next blog. See you in my next blog.








No comments:

Post a Comment