Git Cheat-Sheet

Shell Aliases I Install Everywhere

alias gd='git diff'
alias gc='git commit -v'
alias gs='git status'
alias gl='git log'

# perform 'git add -p' unless
# arguments are supplied
function ga() {
  if [[ $# -gt 0 ]]; then
    git add "$@"
    return $?
  fi
  git add -p
}

Summary since last tag

For writing/reviewing release notes prior to dropping a new tag. Using –first-parent is useful if you’re using merge-commits to simplify the output to just the merges.

git checkout master
git pull
git log $(git describe --tags --abbrev=0)..master --first-parent --oneline

Review current branch

# Review commits since branch-create
git log origin/master..HEAD
# equivalent to
git log HEAD --not origin/master

# Diff with the branch-point with 'master'. This isn't exactly a summary
# of what gets merged-in, but it should give you a good idea.
git diff $(git merge-base HEAD origin/master) HEAD
# If you can remember the difference between '..' and '...' this might
# be easier to type (and is equivalent):
git diff origin/master...HEAD

I’m diffing against origin/master instead of master because if I’m doing a lot of branch-based code-review I might be looking at a branch which is newer than the last time I did a git pull in my local master checkout.

Settings I’m partial to

pull.rebase = true

If I’m working in a topic-branch with another developer I typically want to rebase my local changes on top of their remote changes and avoid merge-commits through normal development.

If we want to land topic into master, or update topic with a newer master I like to see the merge commits (so that we don’t mess-up in progress code-reviews or the like).

I’m happy to rebase like crazy in a topic-branch prior to sending off the branch for review.

interactive.singleKey = true

Necessary. Makes git add -p, git checkout -p and the like more like the darcs single-key prompting.

On Debian/Ubuntu &c this also requires:

sudo apt-get install libterm-readkey-perl

Cherry pick

Git has a notion of a ‘patch id’, which is a hash of a normalized diff (or patch), which can be used to identify rebased or cherry-picked commits as identical to other commits, and internally is used to (for example) skip identical commits during rebase operations.

If you need your own custom-tooling around cherry-picking you might be able to use these semi-stable patch-ids for something useful. An example:

git rev-list $SomeSearchCriteria -- | git diff-tree -p --stdin | git patch-id

This outputs a pair of patch-ids and commit-ids per line, based on the normal sort of commit-walking used by git rev-list.

Links/Tutorials/&c.