DrupalDork.com was shut down on September 18, 2013. This is just a snapshot of the site as it appeared then.

Insanely Useful Tips for Git on the Command Line

I've been meaning to write something up about these for a while, because they have made my development flow so much better.

Tab completion

Arguably, the best thing about git is lightweight branches. But, I had been resisting using them, because it was a pain in the neck to keep track of them and re-type or copy-pasta the name for every checkout or merge. I rely heavily on tab completion in the command line, because I know I'll mistype things otherwise, and spend way too much time trying to figure out why something is broke later on - for example, after mistyping the target of a symlink.

As it turns out, git ships with a script to handle tab completion: you just need to find and enable it.

Since I use Homebrew to install and update packages (Mac users: you should too), my git executable is in /usr/local/Cellar/git/ You need to find git-completion.bash. In my case, it's in a sibling directory: /usr/local/Cellar/git/

Once you find it, include it in your .bash_profile, replacing the path specified here with the location you found:

# Use git auto-completion
source /usr/local/Cellar/git/

Tab completion works for git commands (reset, status, commit, etc) as well as branch names.

Branch name in prompt

This one would not have even occurred to me, but I came across it in a comment on a Lullabot blog post about git. You can show the current branch in the prompt when in a git repo directory, like this:

brockbookpro:multi_smtp (6.x-1.x) $

Again, modify your .bash_profile to add this:

function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
export PS1="\h:\W \$(parse_git_branch)\$ "

If you're working on a project with a lot of branches, this will make life easier.

Add new comment