Git tab completion and prompt change

  |   Source

Git has a number of commands that you use on a regular basis and after a short while you remember them or you create aliases for them in the .bashrc or .bash_alias file and you remember them. Or maybe you don't.

Included in the source code for Git is a bash script called git-completion.bash.

I use this script for 2 things:1) to get tab completion of Git commands (hence the name) but it also provides a way to change the prompt and show that you are in a directory that has a Git repository in it and also what the current branch is.

We will do this in 2 steps. First the tab completion and then modifying the prompt.

Step 1: Install it to get tab completion.

Go to a downloads or tmp directory on your computer and clone the Git source code:

$ git clone git://

Go to the git/contrib/completion directory:

$ cd git/contrib/completion

Copy the git-completion.bash file to your home directory and rename it to a dot file:

$ cp git-completion.bash ~/.git-completion.bash

Edit your .bashrc file and source the .git-completion.bash file by adding the next two lines.

(Should be somewhere near the top of the file or at least before you see the PS1 being defined):

# add git information to the prompt
source ~/.git-completion.bash

Save and close the .bashrc file and activate the new .bashrc file by either:

1)exiting the shell and open a new shell


  1. source the new .bashrc by this command:

    $ source .bashrc

You now have tab completion of the Git commands.

Step 2: Modify the prompt.

Command line prompts are a personal thing so yours may look different then mine.

I have mine adjusted to show me username@hostname:"only the last directory name"

For example, the pwd is:


But the prompt is only:


I can see the full path in the Title bar of the shell and this way, the entry point for the prompt isn't all way to the right of the screen when I am in one of those really deeply nested sub-directories.

Go to Color Bash Prompt to get a much better understanding of how to modify the prompt.

In my .bashrc file, this is what the code for PS1 looks like:

if [ "$color_prompt" = yes ]; then
    PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]$(__git_ps1 "(%s)")\$ '
    PS1='\u@\h:\W$(__git_ps1 "(%s)")\$ '

The $(__git_ps1 "(%s)") at the end but before the $ is the part that displays the git information.

(By the way, that is two underscores before "git" and one underscore between "git" and "ps1")

Now, when I am in a directory that has a Git repository in it, the prompt looks like:

Comments powered by Disqus