Git Cheat Sheet

These are a few of the Git commands I find myself looking up all the time. (Feel free to drop your own suggestions in the comments!)

  • Show the staged changes:
    $ git diff --cached

    Also aliased as:

    $ git diff --staged
  • Find the most recent common ancestor of two Git branches:
    $ git merge-base [branch1] [branch2]
  • Find which branch a commit was originally created on:
    $ git reflog show --all | grep [commit SHA]
  • Find all branches that a commit is on (or that a branch has been merged into):
    $ git branch -a --contains [commit SHA or branch name]
  • Get the diff between two branches:
    $ git diff [branch1]..[branch2]
  • Get the diff between two branches, excluding certain subdirectories:
    $ git diff [branch1]..[branch2] -- . ':!path/to/exclude' ':!other/path/to-exclude/'
    
  • Undo/remove a Git commit that has not been pushed (scary fine print):
    $ git reset --hard HEAD^

     

  • Extracting a Git subdirectory into its own submodule
Advertisements

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 )

Google+ photo

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

Connecting to %s