Git Notes based on Pro Git Book
git add -i
Stage changes interactively
 
git add --patch/-p
Stage files partially. This command allows choosing hunks of file to stage
 
git branch
list all local branches
 
git branch -a
list all local as well as remote tracking branches
 
git branch -v
list branches along with SHA and subject line for each HEAD.
 
git branch -vv
list branches along with SHA and subject line and name of remote tracking branch
 
git branch -u $upstream $localbranch
set up local branch to track upstream branch
 
git checkout --conflict=diff3 filepath 
Checkout file with conflict in diff3 format
$$ git checkout --conflict=diff3 testfile.txt
$$ cat testfile.txt

Hello World
<<<<<<< ours
ABCAAAAAAAAAAAAAAAAA
||||||| base
ABCAA
=======
ABCAADGHLAAAAA
>>>>>>> theirs
      
git checkout --patch/-p filepath
checkout partial changes of a file
 
git clean -n
Print what will be deleted but does not delete anything
 
git clean 
Deletes all files including untracked file but not directories
 
git clean -d -f
Deletes all files including untracked files also any empty directories but does not delete ignored files. Safer alternative is to use git stash -u
 
git clean -d -f -x
Deletes all file including untracked files, also any empty directories also ignored files. Safer alternative is to use git stash --all
 
git clean -i
Clean worksspace interactively
 
git commit
Create a new commit using staged changes
 
git commit -m 'my commit message'
Create a new commit using staged changes and given message
 
git commit -a -m 'my commit message'
create a new commit after staging changes in working directory (except untracked files) and give commit message
 
git commit -a -S -m 'my commit message'
create a new commit after staging changes in working directory (except untracked files) and given commit message, also sign the commit, using gpg
 
git commit -amend 
drops last commit and creates a new commit using current statging area content and msg of last last commit. Allows you to edit commit msg, changes in your last commit.
 
git config --system <property> <value>
set config at sytem level for all users
 
git config --global <property> <value>
Sets config for the current user
 
git config  <property> <value>
sets config current project
 
git config user.signingkey 0A46826A
Setup signing key to use for signing commit and tags
 
git clean -i
Clean worksspace interactively
 
git grep -n div
Searches in current working directory, option -n also prints line numbers
$$ git grep -n div
git.min.js:461:    var divisor = encoding.length;
git.min.js:465:    /* Convert to an array of 16-bit big-endian values, forming the dividend */
git.min.js:466:    var dividend = Array(Math.ceil(input.length / 2));
git.min.js:467:    for(i = 0; i < dividend.length; i++)
git.min.js:469:      dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
git.min.js:473:     * Repea
          
git grep --count div
Show number of matches in each file
$$ git grep --count div
git_notes/gitNotesBackup.html:128
git_notes/git_Add.html:11
git_notes/git_Branch.html:24
git_notes/git_Checkout.html:11
git_notes/git_Clean.html:24
git_notes/git_Commit.html:20
git_notes/git_Config.html:24
git_notes/git_Hooks.html:68
git_notes/git_Log.html:34
git_notes/git_Merge.html:16
git_notes/git_Reflog.html:7
git_notes/git_RevParse.html:7
          
git grep -p/--show-function  {term} [files]
search term in files and also print the hunk header for the section
 
git grep {term/pattern} {tag/ref/SHA} {files}
lets you search for term or pattern in given files in particular tree refered to by tag, reg or SHA
 
git grep --break {term/pattern} {tag/ref/SHA} {files}
break inserts an empty line between matches from different files
 
git grep --heading {term/pattern} {tag/ref/SHA} {files}
groups matches from same files and show file name at the top as a header
 
git grep  git grep -p -e {pattern1} div --and -e {pattern2}  {tag/ref/SHA} {files}
makes sure that both patterns match on same line, () can also be used to group sub expressions
 
pre-commit
Run first. It is used to inspect snapshot. Can be used to check if tests are run, code styling, documentation was applied.

        
prepare-commit-msg
Run before the commit message editor is fired but after the default msg is created Params
  • path to file containing msg
  • type of commit
  • sha of commit if it is an amended commit

        
commit-msg
Can be used to validate final commit msg and abort if it's not correct. Takes path to file containing msg as param

        
post-commit
Can be used for notification of commit. Does not take any parameter.

        
applypatch-msg
First hook run after git am command. Takes one parameter that is path to file containing msg

        
pre-applypatch
Run after patch is applied but before commit is created. Can be used to run tests, inspect working tree. Can be used to abort commit by returning non zero value

        
post-applypatch
Runs after commit is made. Can be used to notify of the commit

        
pre-rebase
Run before you rebase anything. Can be use to halt git rebase. Can be used to disallow rebasing any commit which have been pushed already

        
post-rewrite
Run after commits are rewritten by either git rebase or git commit --amend but not git filter-branch. Param is the command which triggered rewrite. And list of commits as stdin . This can be used to setup working directory after rewrite.

        
post-checkout
Runs after checkout. Can be used to setup working directory, like moving large files , auto generating documentation etc.

        
post-merge
Runs after a successful merge

        
pre-push
Runs after remote refs have been updated but no data has been transmitted yet. It receives name and location of remote as params and list of to be updated refs as stdin

        
pre-auto-gc
Runs before GC takes place and can be used to notify or abort GC based on some criteria.

        
pre-recieve
Runs when data from push is received . can be used to reject push completely

        
update
Similar to pre-recieve but is executed once for each branch. Can be used to reject push for branches selectively

        
post-recieve
Runs after entire process is complete. Can be used to notify usees or other systems. Client waits for this this script to return before disconnecting. So, should not do long blocking tasks.

        
git log
Prints details of all commits reachable from HEAD
commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D

commit 662b3caf91bbb8faf56ef7262615a8828b381be1
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:16 2016 +0530

    Commit C

commit f46d0d9949274bba7f797a5b63bc004f291905b4
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:03 2016 +0530

    Commit B

commit bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A
         
git log --abbrev-commit
Prints details of all commits reachable from HEAD but it prints SHA of commit in abbreviated form
commit af7725c
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D

commit 662b3ca
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:16 2016 +0530

    Commit C

commit f46d0d9
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:01:03 2016 +0530

    Commit B

commit bbb7387
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A
            
git log --oneline
Print abbreviated SHA and commit msg summary in one line
af7725c Commit D
662b3ca Commit C
f46d0d9 Commit B
bbb7387 Commit A
           
git log -g
Print reflog formatted as git log
commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Reflog: HEAD@{0} (Nitin Nizhawan )
Reflog message: checkout: moving from testBranch to master
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D

commit 662b3caf91bbb8faf56ef7262615a8828b381be1
Reflog: HEAD@{1} (Nitin Nizhawan )
Reflog message: checkout: moving from master to testBranch
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:16 2016 +0530

    Commit C

commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Reflog: HEAD@{2} (Nitin Nizhawan )
Reflog message: reset: moving to af7725c
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D

commit 662b3caf91bbb8faf56ef7262615a8828b381be1
Reflog: HEAD@{3} (Nitin Nizhawan )
Reflog message: reset: moving to HEAD^
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:16 2016 +0530

    Commit C

commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Reflog: HEAD@{4} (Nitin Nizhawan )
Reflog message: commit: Commit D
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D
           
git log -g master
Print reflog in the format of git log for only master branch. Shows commits master has pointed to in you local workspace.
commit 20e9855e2b90ee6580c3b8db250dae0d9b18cff1
Reflog: master@{0} (Nitin Nizhawan <abc@example.com>)
Reflog message: commit: Commit E
Author: Nitin Nizhawan 
Date:   Sun Feb 28 11:54:30 2016 +0530

    Commit E

commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Reflog: master@{1} (Nitin Nizhawan <abc@example.com>)
Reflog message: reset: moving to af7725c
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D

commit 662b3caf91bbb8faf56ef7262615a8828b381be1
Reflog: master@{2} (Nitin Nizhawan <abc@example.com>)
Reflog message: reset: moving to HEAD^
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:16 2016 +0530

    Commit C

commit af7725caab2d197f07c72f6d9a4814d5d9ce9e53
Reflog: master@{3} (Nitin Nizhawan <abc@example.com>)
Reflog message: commit: Commit D
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:30 2016 +0530

    Commit D
           
git log refA..refB
Print commits which are reachable from refB but not from refA
$$ git log --graph --pretty=oneline --abbrev-commit --all 
* 20e9855 Commit E
* af7725c Commit D
| * cb2e193 TestBranchB
| * ac4148c Commit TestBranchA
|/  
* 662b3ca Commit C
* f46d0d9 Commit B
* bbb7387 Commit A

$$ git log --oneline master..testBranch
cb2e193 TestBranchB
ac4148c Commit TestBranchA

$$ git log --oneline testBranch..master
20e9855 Commit E
af7725c Commit D

         
git log refB --not refA
Print commits which are reachable from refB but not from refA
$$ git log --graph --pretty=oneline --abbrev-commit --all 
* 20e9855 Commit E
* af7725c Commit D
| * cb2e193 TestBranchB
| * ac4148c Commit TestBranchA
|/  
* 662b3ca Commit C
* f46d0d9 Commit B
* bbb7387 Commit A

$$ git log --oneline testBranch --not master
cb2e193 TestBranchB
ac4148c Commit TestBranchA

$$ git log --oneline master --not testBranch
20e9855 Commit E
af7725c Commit D

         
git log ^refA refB
Print commits which are reachable from refB but not from refA
$$ git log --graph --pretty=oneline --abbrev-commit --all 
* 20e9855 Commit E
* af7725c Commit D
| * cb2e193 TestBranchB
| * ac4148c Commit TestBranchA
|/  
* 662b3ca Commit C
* f46d0d9 Commit B
* bbb7387 Commit A

$$ git log --oneline ^master testBranch
cb2e193 TestBranchB
ac4148c Commit TestBranchA

$$ git log --oneline ^testBranch master
20e9855 Commit E
af7725c Commit D

         
git log refA...refB
Print commits which are reachable from refA or refB but not both
$$ git log --graph --pretty=oneline --abbrev-commit --all
* 20e9855 Commit E
* af7725c Commit D
| * cb2e193 TestBranchB
| * ac4148c Commit TestBranchA
|/  
* 662b3ca Commit C
* f46d0d9 Commit B
* bbb7387 Commit A

$$ git log --oneline master...testBranch
20e9855 Commit E
cb2e193 TestBranchB
ac4148c Commit TestBranchA
af7725c Commit D

$$ git log --oneline master...testBranch --left-right
< 20e9855 Commit E
> cb2e193 TestBranchB
> ac4148c Commit TestBranchA
< af7725c Commit D
         
git log --show-signature
Also show signature information
 
git log -Sterm 
Search for commits which either removed or added "term" to the repo
 
git log -L {start_line_no},{end_line_no}:{filepath} 
Search commits which modified lines starting from start_line_no to end_line_no in file filepath
 
git log -L /{start_regex}/,/{end_regex}/:{filepath} 
Search commits which modified lines starting lines which match start_regex to line which match end_regex in file filepath
 
git merge abc
merge abc branch in current branch
 
git merge --verify-signatures abc
merge abc branch in currect branch and also verify signature of each commit else abort
 
git merge --verify-signatures -S abc
merge abc branch in currect branch and also verify signature of each commit else abort, also sign the resulting commit usin gpg key
 
git reflog
Show history of where the HEAD and branch refs have been in local repository. Information stored as reflog is strictly local.
af7725c HEAD@{0}: checkout: moving from testBranch to master
662b3ca HEAD@{1}: checkout: moving from master to testBranch
af7725c HEAD@{2}: reset: moving to af7725c
662b3ca HEAD@{3}: reset: moving to HEAD^
af7725c HEAD@{4}: commit: Commit D
662b3ca HEAD@{5}: commit: Commit C
f46d0d9 HEAD@{6}: commit: Commit B
bbb7387 HEAD@{7}: commit (initial): Commit A
          
git rev-parse master
Show commit pointed to by ref
af7725caab2d197f07c72f6d9a4814d5d9ce9e53
          
git show bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Show details of a commit SHA along with diff
commit bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A

diff --git a/testfile.txt b/testfile.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/testfile.txt
@@ -0,0 +1 @@
+Hello World
        
git show bbb738
Show details of a abbreviated commit SHA along with diff
commit bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A

diff --git a/testfile.txt b/testfile.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/testfile.txt
@@ -0,0 +1 @@
+Hello World
        
git show master
Show commit details along with diff pointed to by a ref
commit bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A

diff --git a/testfile.txt b/testfile.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/testfile.txt
@@ -0,0 +1 @@
+Hello World
        
git show HEAD@{7}
Show details of a commit along with diff pointed to by a reference in reflog
commit bbb738746b14e8e4ac09a443e9ac1c931ad61d07
Author: Nitin Nizhawan <abc@example.com>
Date:   Sat Feb 27 21:00:29 2016 +0530

    Commit A

diff --git a/testfile.txt b/testfile.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/testfile.txt
@@ -0,0 +1 @@
+Hello World
        
git show master@{3.hour.41.minute.ago}
Show details of a commit where master was 3 hour 41 minutes ago. Other examples include ref@{yesterday}
commit f46d0d9949274bba7f797a5b63bc004f291905b4
Author: Nitin Nizhawan 
Date:   Sat Feb 27 21:01:03 2016 +0530

    Commit B

diff --git a/testfile.txt b/testfile.txt
index 557db03..bc1c618 100644
--- a/testfile.txt
+++ b/testfile.txt
@@ -1 +1,2 @@
 Hello World
+ABC
        
git show --oneline HEAD^2~1
Show commit pointed to by parent reference
$$ git log --graph --pretty=oneline --abbrev-commit 
*   72cdd46 Merge branch 'testBranch'
|\  
| * cb2e193 TestBranchB
| * ac4148c Commit TestBranchA
* | 20e9855 Commit E
* | af7725c Commit D
|/  
* 662b3ca Commit C
* f46d0d9 Commit B
* bbb7387 Commit A	

$$ git show --oneline HEAD^2~1
ac4148c Commit TestBranchA
diff --git a/testfile.txt b/testfile.txt
index 0685a0a..965369c 100644
--- a/testfile.txt
+++ b/testfile.txt
@@ -1,2 +1,2 @@
 Hello World
-ABCAA
+ABCAADGHL

$$ git show --oneline HEAD^1~1
af7725c Commit D
diff --git a/testfile.txt b/testfile.txt
index 0685a0a..6a31125 100644
--- a/testfile.txt
+++ b/testfile.txt
@@ -1,2 +1,2 @@
 Hello World
-ABCAA
+ABCAAAAAAA
     
git stash --patch/-p
Stash partial changes.
 
git stash --keep-index
Do not stash changes which have been staged
 
git stash apply --index
Apply stashed changes and stage changes which were staged at the time of stashing
 
git stash --include-untracked/-u
Also stash untracked files
 
git stash branch <branchname> 
Creates a branch with stashed changes. Branch is created from same commit where the HEAD was when changes were stashed. This also removes stash from stash list.
 
git stash --all
Stashes everything including untracked files and even ignored files. This can be used as safe git clean -d -f -x
 
git tag v1.2
add a light weight tag. do not specify -a,-m or -s flag
 
git tag -a v1.2
add an annotated tag, you can also specify a message
 
git tag -s v1.2 
add a tag and sign it using gpg
 
git tag -v v1.2
verify signature of tag v1.2
 
git tag
list all tags