Auto Complete for Git

One of the first things that I do when setting up a new machine for developing on is setting up the ability to use the auto-complete in Terminal when typing out branches.

I never again have to remember the whole name of the branch when switching and it saves so much time (if you are like me and can't use GUIs for Git). Here are the steps below:

1) Download the script from Github. You will see that it itself has instructions and the following steps are basically taken from there.

2) Move this file to somewhere in your home directory e.g. ~/.git-completion.bash.

3) Add source ~/.git-completion.bash to either ~/.bashrc on Ubuntu or ~/.bash_profile on Mac.

4) Restart Terminal and that is it. When you use Git commands, you can use TAB to auto complete commands and the branches!

Bonus Round - Show Current Branch

I also like to have the current branch that I am working on displayed in the terminal. So I also add the following:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

This displays the branch in the Terminal like so:

I think you can get even more details by using their git-prompt.sh script. I will leave details of that here as I have never used it.

So here you have 2 very simple tweaks that I think make a real difference to productivity.


© 2012-2023