Git Tips
This page is meant to be a collection of the useful bits that I’ve been learning about git. In particular, commands and their options that are useful, but wouldn’t necessarily know because I don’t read man pages all the time.
Set the upstream branch
Git grew a --set-upstream
option for branch in 1.7.0. Usage is simple:
For example:
Finding which tag contains a particular commit
For example:
If you know an idea of which set of tags it’s in, then you can use
--match
to narrow down the search and make things go a bit
faster:
Investigating a conflicted merge
Greping through history
To search all history:
To search part of the history:
Set the mtime to the last commit that touched a file
Use the following as a post-checkout
hook:
Determining how far ahead/behind a branch is
The first number is the number of commits behind upstream, and the second number is the number ahead of upstream. For instance:
would mean that you’re 2 commits behind upstream, but have 3 commits that are not upstream. In other words, this example shows a divergent branch.
History simplification
Git, by default, tries to simplify history whenever possible. The best description of this can be found here: http://thread.gmane.org/gmane.comp.version-control.git/89400/focus=90659. Thanks to Junio Hamano for the link.
How to write a commit message for Git
Not only will this help you when using various options in Git (oneline summaries, etc.), but it is sound advice on writing commit messages in general: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html. Thanks to GitHub for providing the link.
Relative commit pathspec syntax
Let’s say you wanted to see what path/to/somefile.py
contained on
master in the HEAD revision. You could use this:
from anywhere in the working tree. That’s kind of painful if you are already
in the path/to
directory though. Instead, use the relative version of
the syntax: