Skip to content

Instantly share code, notes, and snippets.

@jyek
Created March 24, 2014 21:38
Show Gist options
  • Save jyek/9749723 to your computer and use it in GitHub Desktop.
Save jyek/9749723 to your computer and use it in GitHub Desktop.

Git Cheat Sheet

Although there are enough resources on the web about Git, I will keep this one for my own reference.

http://cheat.errtheblog.com/s/git

TOC


Legend

  • index: staging area (Imagine you are loading sand into the truck with bucket. Well, the bucket is like index :))
  • <sha1>: sha1 hash of commit
  • <file>: type the path to the file in question (path/to/file.ext)

Info

Find commits where files were deleted

git log --diff-filter=D --summary

Checkout deleted file in the working tree

git checkout <sha1>^ <file>

Only show the content of a file from a specific revision

git show <sha1>^:<file>

Show file's history

git log -p <file>

Show changes on a branch that is not merged upstream

git cherry <upstream_branch> <new_branch>
git log <upstream_branch>..<new_branch>

Show log with changed files

git log --name-only
git log --name-status
git log --stat

Get latest tag in the current branch

git describe --exact-match --abbrev=0
git describe --abbrev=0 --tags

TOC

Adding

Add changes to the index chunk by chunk

git add --patch <file>
  • y: stage this chunk
  • n: do not stage this chunk
  • s: split this chunk into smaller chunks
  • e: edit this chunk

TOC

Branching

Create local branch

git checkout -b <branch_name>

or

git branch <branch_name>
git checkout <branch_name>

Delete local branch

git branch (-d | -D) <branch_name>

TOC

Managing

Copy commit range from one branch to another

cherry-pick A..B    # Git 1.7.2+, commit A should be older than B

TOC

Undoing

Undo a merge or pull

Check out git reset for great explanation and examples.

git reset --hard

Undo a merge or pull inside a dirty work tree

Check out git reset for great explanation and examples.

git reset --merge ORIG_HEAD

Revert a bad commit

git revert <sha1>

Checkout a deleted file into the work tree

git checkout <sha1>^ -- <file>

TOC

Remotes

Create local branch then push to the remote (without tracking !!!)

git checkout -b <branch_name>
git push origin <branch_name>

Crete a new local branch by pulling a remote branch

git pull origin <branch_name>                                 # without tracking
git checkout --track -b <branch_name> origin/<branch_name>    # with tracking

Track a remote branch with an existing local

git branch --set-upstream <branch_name> origin/<branch_name>

Delete remote branch

git push origin :heads/<branch_name>

or

git push origin :<branch_name>

Prune remote-tracking branches that are deleted from a remote repo

git remote prune origin

Change remote URL

git remote set-url origin http://new-example.com/repo.git

TOC

Submodules

Update submodules

git submodule foreach 'git checkout master && git pull origin master'

Update submodule's URL

Edit the .gitmodules file, then run:

git submodule sync

Remove submodule

  • remove the submodule's entry in the .gitmodules file
  • remove the submodule's entry in the .git/config
  • run git rm –cached path/to/module - without a trailing slash!
  • remove the submodule from the filesystem, run rm -rf path/to/module/
  • commit changes

TOC

Additional resources

TOC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment