1 minute read

I’ve been using git for various projects and want to share tips for making it more convenient.

One of git’s advantages is cheap branches. A branch is just a file in .git/refs/heads containing the id of the last commit, so creating a new branch takes very little time. With such capabilities, most programmers use branches extensively.

Whatever model you choose for branches, you can eventually forget which branch you’re on and commit code to the wrong place. Although this is easily fixed, you can avoid it by adding branch information to your bash prompt.

Installing git bash completion

Git includes several useful scripts, including git-completion.bash. After loading it, bash completion will include git commands, local and remote branch names, tags, and more. The script also defines useful functions for getting environment information.

% git 
add           -- add file contents to index
am            -- apply patches from a mailbox
apply              -- apply patch to files and/or to index
archimport         -- import an Arch repository into git
archive       -- create archive of files from named tree
bisect        -- find, by binary search, change that introduced a bug
blame              -- show what revision and author last modified each line of a file
br       -- alias for 'branch'
branch        -- list, create, or delete branches
bundle        -- move objects and refs by archive
cat-file           -- provide content or type information for repository objects
check-attr          -- display gitattributes information
check-ref-format    -- ensure that a reference name is well formed
checkout      -- checkout branch or paths to working tree
checkout-index     -- copy files from index to working directory
cherry             -- find commits not merged upstream
</pre>


If you use MacPorts, ensure git-core is installed with the +bash_completion variant:

% sudo port install git-core +bash_completion
---> Computing dependencies for git-core
---> Cleaning git-core
If you installed git from source, search for the script:
% mdfind git-completion
/usr/local/Cellar/git/1.8.2/etc/bash_completion.d/git-completion.bash
### Setting up bash prompt Add the following lines to ~/.bashrc:
source /path/to/git-completion.bash

function __set_prompt
{
	# Configure Colors:
	WHITE='033[1;37m'
	LIGHTGRAY='033[0;37m'
	GRAY='033[1;30m'
	BLACK='033[0;30m'
	RED='033[0;31m'
	LIGHTRED='033[1;31m'
	GREEN='033[0;32m'
	LIGHTGREEN='033[1;32m'
	BROWN='033[0;33m'
	YELLOW='033[1;33m'
	BLUE='033[0;34m'
	LIGHTBLUE='033[1;34m'
	PURPLE='033[0;35m'
	PINK='033[1;35m'
	CYAN='033[0;36m'
	LIGHTCYAN='033[1;36m'
	DEFAULT='033[0m'

	export PS1="[${GREEN}]u.h[${LIGHTBLUE}]"' $(__git_ps1 "(%s) ")'"[${BROWN}]w [${DEFAULT}]$ "
}
__set_prompt
**Update:** Enclose colors in PS1 with [], otherwise bash will incorrectly calculate prompt length and handle line wrapping incorrectly. After setup, if the current directory is under git control, the command line will display the current branch name.

Comments