Autocomplete git branches and commands in bash

February 10, 2016

The tab completion for commands in bash is awesome, but it does not work for bash out of the box. Lucky for us it is relatively easy to get bash completion working.

You will need two things, a copy of the completion script and an update to your bash profile.

The completion script is available at: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

Then in your .bashrc (or .bash_profile, or .profile, whatever you are setup for) add the following:

[bash]
if [ -f ~/.bin/git-completion.bash ]; then
. ~/.bin/git-completion.bash
fi

[/bash]

Be sure the path to the git-completion.bash file is correct for your system. On mine I keep extra commands in ~/.bin.

Start using your new completion with:

[bash]
source ~/.bashrc
[/bash]

Now simply type ‘git’ and hit tab to see all the commands available.

When switching branches type ‘git checkout’ and hit tab to see a list of the branches.

Leave a Reply

Your email address will not be published. Required fields are marked *