Configuring Git

When starting to use Git on a new computer there are several steps that I always take before I start work. They make life a little easier when working with Git in a terminal.

Coloured output

Colour highlighting can often make things easier to read. Turn it on:

$ git config --global color.ui "auto"

You'll find the results of git config commands stored in ~/.gitconfig. It's very easy to edit it by hand if you want to undo any changes.

Setup your global profile

Whenever you commit to a repository git records your username along with the commit. If you're committing to a public repository it can also be useful to include your contact details, so let's configure git to use our real name and email address:

$ git config --global user.name "Your Name"
$ git config --global user.email "your@email.address"

Those commands configure your "global name" and "global email"; you can also configure them on a per-project basis by running the command from within a git repository, and omitting the --global switch.

Ignore whitespace

By default git is a bit fussy about whitespace and will print a warning when merging files where whitespace differs. If you're not working with files where whitespace is particularly significant you can skip these warnings:

$ git config --global apply.whitespace nowarn

Show the git branch in your Bash prompt

Branches are so easy to work with in git that you will probably find yourself switching between them very frequently. It's always nice to know which branch you're in, and many people update their shell prompt to show them. My prompt looks like this:

poncho (master)$

where "poncho" is my hostname and "master" is my current branch. When I'm not in a git directory then my prompt reverts to poncho$.

Here's one way to do it in Bash. If your git installation has made the __git_ps1() function available in your shell you can just set your PS1 variable in your ~/.bashrc file. Otherwise you may need to define __git_ps1 as well:

__git_ps1 () 
{ 
    local b="$(git symbolic-ref HEAD 2>/dev/null)";
    if [ -n "$b" ]; then
        printf " (%s)" "${b##refs/heads/}";
    fi
}

PS1="\h\$(__git_ps1)$ "

If you like a coloured prompt, check out Bedazzle Your Bash Prompt with Git info.