My Git Cheat Sheet

Printer-friendly versionPDF version

Following is my cheat sheet that I extracted while reading the git tutorial So I just listed the commands minus all the text there. If you have read the book than below will a quick reminder of the various commands there. Not all commands are listed but the more interesting only.

Viewing Changes

  • git status
  • git status -s  // Left column is Staging directory and right column in Working Directory (WD)
  • git diff    // WD with Staged
  • git diff --staged    // Staged with last commit

Removing / Moving Files

  • Use "git rm" to remove file. If OS rm used than "git rm" still neded.
  • git rm --cached  // To delete from Index but not working dir e.g. accidently added files like logs etc
  • git mv a.txt b.txt // Short for OS rename and than git rm a.txt and git add b.txt

Commit History

  • git log
  • git log -p -2    // where numeral is last 2 commits
  • git log --pretty=format:"%h %s" --graph  //Graphs the commits as tree
    • --pretty=online/short/full/fuller
  • git log --name-only
  • git log --name-status
  • git log --since "2017-08-23"
    • git log --since "1 years 4 days ago"
  • git log --grep string  // Search in commit comments
  • git log -S code  // Search commits for code string
  • git log --oneline --decorate  // Shows where branch pointers are at.
  • git log --oneline --decorate --graph --all  // graph view with above command
  • git log --no-merges issue54..origin/master   // Show list of commits on latter branch that are not on issue54 branch
    • git log --no-merges issue54..origin/issue54  // To see what your collaborator pushed differnet from your local branch now

Undoing Changes

  • git commit --amend  // Fix last commit to maybe add files or change commit msg
  • git checkout -- filename   // Unmodiying a modified file to last commited or cloned state
  • git reset HEAD filename  // Unstaging a staged file

Reverting to a previous commited version

  1. git log --oneline --decorate   // Use to find out the hash of the commit to where you want to revert
  2. git checkout b0e9481 --  /Path/to/file
    1. If you want to revert to the version before the commit hash use b0e9481~1

Working with Remotes

  • git remove -v  // Shows the remote project you cloned, atleast origin is shown if you did just git clone with default options
  • git remote add friends-repo url // add a new remote repo
    • git fetch  // fetch metadata of new repo but nothing happens to WD or Index
  • git remote show origin  // Shows detail or repo, also which local branch is configured to pull and push from which remote branch
  • git remote rename origin newOrigin
  • git remote remove friends-repo


  • git tag // shows all tags alphabetically
  • git tag -l "1.1"  // show tags with this pattern
  • git tag -a v1 -m "First release"  //annotated -a tag
  • git tag v1.1  //lightweight tag
  • git tag -a v1.1 zt4r5e    // extract the hash using  :- git log --pretty=online
  • git push origin v1  /push tag v1
  • git push origin --tags   // push all tags
  • git checkout -b version1Branch v1   // checkout tag v1 in local branch


  • git config --global checkout  // this makes "git co"  an alias
  • git config --global alias.last 'log -1 HEAD'   // see last commit alias


  • git branch   // shows all branches
  • git branch --merged   // shows all branches that are merged and safe to delete
  • git branch --no-merged   // branch deletion fails for these as not merged, git branch -d BUT git branch -D works

Remote Branching

  • git clone -o ohmyOrigin  // to name the remote from default name "origin" to the given name
  • git push origin myfix   // push local branch myfix to a remote branch myfix
  • git push origin myfix:ourfix  // push local branch myfix to a remote branch ourfix
  • git checkout --track origin/serverfix    //  One command to do git checkout -b serverfix origin/serverfix and create a tracking branch (a local branch that tracks a remote)
  • git checkout -b sf origin/serverfix   // to create a diff local name than remote tracking branch
  • git branch -u origin/serverfix   // Use this to setup your existing local branch to track a remote one
  • git branch -vv  // Shows what tracking branches you have setup
    • git fetch --all ; git branch -vv  // Best practise, first get server data, otherwise seeing local cached state
  • git push origin --delete  // Delete a merged branch on server

Renaming a remote and local branch

  1. git fetch --all
  2. git branch -m branchToMoveOldName  // -m is move branch option
  3. git push origin :branchToMoveOldName  branchToMoveNewName 
    1. git branch -avv   // This will now show a message like  * branchToMoveNewName     33298c8 [origin/branchToMoveOldName  : gone]
  4.  git push origin -u branchToMoveNewName       // Will show Branch branchToMoveNewName set up to track remote branch branchToMoveNewName from origin.
    1. git branch -avv   // This will now show a message like  * branchToMoveNewName    33298c8 [origin/branchToMoveNewName]

Basic Merging


  1. git checkout master // branch where you want to merge
  2. git merge mybranch   // Execute merge command with your branch name
  3. git mergetool   // optional visual conflict resolution

Branch Rebasing

Rebasing Def: Taking a patch of a commit and applying it to an ancestor. So rebase takes all the changes from a branch and applies them to another one you want to. Sequence is as below:

  1. git checkout experiment
  2. git rebase master
  3. git checkout master
  4. git merge experiment

Adv. is clean linear history even though work happened on different branch in parallel

  • git rebase master experiment // Shortcut for command#1 and #2 above


  • git config --global credential.helper cache  // Caching your credentials so as not to type them when connecting to remote via https


Top level category:

Add new comment