My Git Cheatsheet

Unlike Mercurial, Git is a far more complex and complicated beast. This post is a work-in-progress to gather the commands that I end up discovering.

$ git reset HEAD that_file
  • How to revert a staged or modified file back to its last commit:
$ git checkout -- file_path
  • How to revert all modified tracked files:
$ git reset --hard
  • How to revert the effect of a particular commit:
$ git revert sha_hash_of_that_commit
  • How to remove all untracked files:
$ git clean -fd
  • Create branch at current revision or branch:
$ git branch branch_name
  • Create branch at specific revision:
$ git branch branch_name hash_of_revision
  • Rename current branch:
$ git branch -m new_branch_name
  • Rename some branch:
$ git branch -m old_branch_name new_branch_name
  • How to delete a local branch:
$ git branch -d branch_name

Note that you cannot delete the current branch. Checkout to a different branch and then delete the branch you wanted to. This is because imagine chopping off the branch of a tree you are sitting on. You will fall down!🙂

  • How to merge other branch to master branch:
$ git checkout master_branch
$ git merge other_branch
  • How to resolve merge conflicts using GUI tool:
$ git mergetool
  • How to see all info about remote, its branches and push-pull to it:
$ git remote show remote_name
  • How to rename a remote:
$ git remote rename old_remote_name new_remote_name
  • How to checkout a remote branch:
$ git checkout -b local_branch remote_name/remote_branch
  • How to push branch on local to branch on remote:
$ git push remote_name local_branch_name:remote_branch_name
  • How to revert non-staged non-committed changes:
$ git checkout that_file
  • How to delete a remote branch:
$ git push remote_name --delete remote_branch
  • How to rebase my_branch on top of main_branch:
$ git checkout my_branch
$ git rebase main_branch
  • How to checkout a remote branch to a new local branch (of same name) with tracking:
$ git checkout -t remote_name/remote_branch
  • How to make an existing local branch track a remote branch (or change remote tracking branch):
$ git branch local_branch -u remote_name/remote_branch
  • How to rename a remote branch:
$ git push remote_name remote_name/old_branch:refs/heads/new_branch :old_branch


  • How to list all submodules:
$ git submodule status --recursive
  • How to fetch all submodules:
$ git fetch --all --recurse-submodules
  • How to update all submodules to revision required by main module revision:
$ git submodule update --recursive
  • How to initialize all submodules the first time and update them to revision required by main module revision:
$ git submodule update --init --recursive


  • How to view HEAD history of your local repository:
$ git reflog
  • Find which commit added or deleted a certain string:
$ git log -S foobar
  • Get the history of a function:
$ git log -L :funcname:filename
  • How to change the last commit message:
$ git commit --amend
  • Show who last modified lines of a file:
$ git blame -C -L 234,234 foobar.cpp

Leave a Reply

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

You are commenting using your 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