Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Git cheat sheet
- my normal flow?
- good commit messages ******
- searching commit messages (group by keywords)
- searching code *
- working with history (viewing, time traveling)
- rebasing (for pulling & squashing, splitting a commit) *
- undoing local commits (soft, hard reset)
- forgot to add / change message (amend)
- LOST commits? *
- WHY and WHO changed this (blame) *
- WHAT (diffing)
- github shortcuts *
- purging & rewriting history (password file) *
- ack vs grep *
# pull --rebase
$ git pull --rebase upstream develop
$ git config --global pull.rebase true
# Undo 1 commit locally (hasn't been pushed remote yet)
$ git reset --soft HEAD^ # undo commit, and keep changes in staging area (index)
$ git reset --hard HEAD^ # undo commit, and discard changes entirely
$ git reset # unstage all files in staging area
# Remove a commit from past history (not remote yet)
$ git rebase -i HEAD~3 # go back 3 commits from current
$ git rebase --onto <commit-id>^ <commit-id> # squash this commit
# Ignore temporarily (great for hack to make it work locally, that you don’t want to commit)
$ git update-index —assume-unchanged fileName.txt
$ git update-index —no-assume-unchanged fileName.txt
# Show history for this file
$ git log -p index.js
$ git log --since=2.days
$ git log --author=jacharles
# Search commit messages for grep string
$ git log --grep 'fix('
# Search the commit messages for phrase 'hackyFix' from HEAD to 1ed2342 commit.
git log -i --grep hackyFix 1ed2342..HEAD --no-merges --stat
# Search for commit that changed this code *** AWESOME ***
$ git log -S 'if (isHeightNormal) {'
$ git log -S 'if (isHeightNormal) {' -p
$ git log -S 'if (isHeightNormal) {' -p -- index.js
# Git aliases
$ cat ~/.gitconfig
$ git config --global alias.ll= some command here
$ git config -l
# Lost commits
$ git reflog
$ git fsck --lost-found
### VI enhancement for commit messages (don't use -m to see this)
# If you use the command line to write your commit messages, this will help.
# Add this to `~/.vimrc` to provide colors, spellcheck, widthLimits. Also tells you how long your subject line should be.
syntax on
autocmd Filetype gitcommit setlocal spell textwidth=72
# Further reading:
# - - Short read on the guts of git
# - - Excellent Git courses on Codeschool
# - - Simple git reference site by GitHub
# - - Great site for learning git gradually
# - - Free Git book
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment