Top and Most commonly used Git commands you should know


Currently, I am using MacOS-X 10.9; though I believe the following commands will work irrespective of your Hardware and OS. I grouped the commands by their relevancy and their necessity base (basic commands first). The target audience are developers, who know git theoretically and also use git in day to day work but having hard time remembering the commands.

After successful Installation:

check to see where is the git located

~$ which git
/usr/bin/git

the version of the git installed

~$ git --version
git version 1.8.3.4 (Apple Git-47)

Configuration:

System wide git configuration file path: /etc/gitconfig
This file is not used or modified commonly.

file path for Windows: Program files\Git\etc\gitconfig

User git configuration file path: ~/.gitconfig
file path for Windows: $HOME\.gitconfig

Project git configuration file path: PROJECT/.git/config

set user name (User configuration)

~$ git config --global user.name "YOUR USER NAME" 

--global [indicates User configuration, not System configuration]
--system [indicates System wide configuration]

set user email id (User configuration)

~$ git config --global user.email "YOUR EMAIL ID"

set default text editor for git

~$ git config --global core.editor "textedit -wl1"

-w [indicates, git should wait for next task until editor is closed]
l1 [indicates, the text editor will put the cursor at line 1 when starts]

use colors with git command line

~$ git config --global color.ui true

Current status of configuration

~$ git config --list
user.name=shahab.uddin
user.email=tausiq19@gmail.com
editor="textedit -wl1"
color.ui=true

Auto Completion for Git:

Windows already have Auto completion feature already. So the instruction are for Mac and Unix user only

Download the completion file form Github

~$ cd ~
~$ curl -0L https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

Rename file

~$ mv ~/git-completion.bash ~/.git-completion.bash

Open bash profile file

~$ nano .bash_profile

Edit bash profile file
add this code to the file

if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi

now hit: ctrl + x to close the file
now hit: y to say, yes I want to save the file
now hit: enter to go back
restart terminal (quit and open again)

Git Help:

Open git help

~$ git help

Open git help for specific command

~$ git help log

hit f to move forward
hit b to move backward
hit q to quit

Starting Git

Initialize a project
Select a folder where you would like to create a project (that folder will be tracked by Git).
On terminal go to that folder (write cd and drag drop the folder)

~$ git init
Initialized empty Git repository in /Users/Documents/Sourcecode/Git/first_git_project/.git/

Look inside what Git Initialized

~$ ls -la
total 0
drwxr-xr-x   3 Shahab  staff  102 Nov 24 22:17 .
drwxr-xr-x   3 Shahab  staff  102 Jul 14 05:12 ..
drwxr-xr-x  10 Shahab  staff  340 Nov 24 22:15 .git
~$ ls -la .git/
total 24
drwxr-xr-x  10 Shahab  staff  340 Nov 24 22:15 .
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:17 ..
-rw-r--r--   1 Shahab  staff   23 Nov 24 22:15 HEAD
drwxr-xr-x   2 Shahab  staff   68 Nov 24 22:15 branches
-rw-r--r--   1 Shahab  staff  138 Nov 24 22:15 config
-rw-r--r--   1 Shahab  staff   73 Nov 24 22:15 description
drwxr-xr-x  11 Shahab  staff  374 Nov 24 22:15 hooks
drwxr-xr-x   3 Shahab  staff  102 Nov 24 22:15 info
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:15 objects
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:15 refs

Add files to Git

~$ git add .

. [indicates that all files inside the directory will be added to Git]

~$ git add firstFile.txt

First commit with Git

~$ git commit -m "Initial commit"
[master (root-commit) 468be11] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 firstFile.txt

-m [indicates that we are going to add a message next]

Few tips on commit messages

  • write commit messages is present tense, not past tense.
  • write “fix bug” or “fixes bug”, not “fixed bug”
  • you can add bullet points using asterisks or hyphen
  • Its a good idea to add ticket tracking numbers/ bug report number with commit
  • Be clear and descriptive
  • for example,
    Bad: “Fix typo”
    Good: “Add missing > in project section of HTML”
  • another example,
    Bad: “Update login info”
    Good: “Change user authentication to Blowfish”

A good commit message example

t23094 – Fixes bug in admin logout

When an admin logged out of the admin area, they could not log in to the members area because their session[:user_id] was still set to the admin ID. This patch fixes the bug by setting session[:user_id] to nil when any user logs out of any area.

Git Log

View the last commits

~$ git log
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the last commits upto limited number

~$ git log -n 1
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

-n 1 [it will show the last 1 commit]

View the last commits since up to a date

~$ git log --since=2013-11-23
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the last commits until a date

~$ git log --until=2013-11-25
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the commits of a particular author

~$ git log --author="shahab.uddin"
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the commits with a regular expression

~$ git log --grep="Init"
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

Find out where is the HEAD pointing now

~$ git log HEAD
commit 468be1105dc2190f76335fcdb3f708a58ecaafaf
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sat Dec 14 00:22:21 2013 +0600

    Initial commit

Git Diff

View the difference of the working directory

~$ git diff
diff --git a/firstFile.txt b/firstFile.txt
index 55d1db5..8ee1b5a 100644
--- a/firstFile.txt
+++ b/firstFile.txt
@@ -1 +1,3 @@
 This is my First File.
+
+I have changed this portion of text.

Scenario
1. You have created a file (firstFile.txt)
2. made some changes to that file
3. add it to the staging index ( using, git add . or, git add firstFile.txt )
4. make some more changes to firstFile.txt
5. Now you would like to view the difference between the staged index and working directory
6. the above command means, show me the difference between the staging index and present working copy

[to see the difference of a specific file, use git diff firstFile.txt ]

View the difference of the staging index

~$ git diff --staged
diff --git a/firstFile.txt b/firstFile.txt
index 55d1db5..8ee1b5a 100644
--- a/firstFile.txt
+++ b/firstFile.txt
@@ -1 +1,3 @@
 This is my First File.
+
+I have changed this portion of text.

Scenario:
1. you have added a file to the staging index, for the 2nd time
2. now you would like to view the changes you made on the 2nd time
3. the above command means, what are the changes in my repository and staging index

Remove file from Git

Remove file form Git repository

Removing a file from git repository is a two step process.
1. remove the file from git repository
2. make a commit that you have removed the file

~$ git rm firstFile.txt
rm 'firstFile.txt'
~$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    firstFile.txt
#
~$ git commit -m "first file removed"
[master c80278c] first file removed
 1 file changed, 3 deletions(-)
 delete mode 100644 firstFile.txt

To be continued

Acknowledgment: Kevin Skoglund from lynda.com

Advertisements

2 thoughts on “Top and Most commonly used Git commands you should know

  1. Dear Sirs,I noticed your article named “Top and Most commonly used Git commands you should know | Solved Programming Problems” like every week. Your story-telling style is awesome, keep it up! And you can look our website about 李宗瑞影片.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s