Eric Goodwin

Git Auto Completion On OS X
08/04/09

It may be a little hidden but Git actually comes with auto completion, you just have to set it up.

If you have the Git source on your computer then you are good to go. If not, you are going to have to download it. After downloading Git we are going to copy the git-completion bash file from the contrib/completion directory into our home directory, prepending it with a period so that it's hidden.

~ > git clone git://git.kernel.org/pub/scm/git/git.git git
~ > cp git/contrib/completion/git-completion.bash ~/.git-completion.bash

Depending on how you've setup your bash startup files the next couple of steps may differ. If you want a great guide on setting up your bash startup files, check out this article.

The first thing we are going to do is include git-completion.bash in our ~/.bash_profile. Look for the .bashrc source include and put git-completion right before it.

# ~/.bash_profile
source ~/.git-completion.bash
source ~/.bashrc

Now you just need to reload the bash_profile and auto complete is ready to go.

~ > . ~/.bash_profile

Wasn't that easy. While we're messing around in the bash startup files though, why don't we add another great little feature. Git-completion.bash includes a method (__git_ps1) to find your current branch and print out it's name. We are going to append the branch name to our command prompt so we can always tell what branch we are working in.

Open your .bashrc files and search for the 'export PS1' line and replace it with the following.

# ~/.bashrc
export PS1='\w$(__git_ps1 "(%s)") > '

After reloading your ~/.bashrc you will have your branch names showing up in the command prompt whenever you are in a Git project. No need to call git-branch to see what branch you are in anymore.

~/projects/TestGit(master) > git checkout rails20 
Switched to branch "rails20"
~/projects/TestGit(rails20) >

If you run into any trouble, check out the git-completion.bash file. It has some good instructions on how to use it at the top of the file.

6 comments

Comments

  1. 08/04/18 - Rob O Says:

    Thanks for the post on how to set up git completion. Even simple tasks need to be explained sometimes.

    It seems like there is a lot of stuff in the git-completion file. It would be cool to find out what the rest of it is for.

  2. 08/04/19 - Eric Goodwin Says:

    Glad the post was able to help you out Rob. I believe the rest of the methods in git-completion.sh are just the auto complete methods for all the git commands. I had a quick look over the file and unfortunately didn’t find any more nice little helpers.

  3. 08/04/19 - elliottcable Says:

    Nice post! Didn’t work for me (long-standing broken bash-completion issues), but is cool nonetheless (-:

    Earned you a spot on my ‘probationary’ RSS feed list, keep up the good posting d-;

  4. 08/04/23 - Dean Strelau Says:

    You need to make sure that you backslash-escape the $ before the __git_ps1 call:

    export PS1='\w\$(__git_ps1 "(%s)") > '

    Otherwise, the $() is evaluated when your profile is sourced, meaning changing into/out of a git repo doesn’t update the value.

  5. 08/04/27 - Tomas Horacek Says:

    Thanks for this post! If you are using MacPorts install Git using:

    port install git-core +bash_completion

    and add to # ~/.bash_profile :

    source /opt/local/etc/bash_completion.d/git

  6. 08/05/15 - julien palmas Says:

    Thx for the great post ! Definitely helps me move from svn to git ;-)

Have your say

A name is required. You may use HTML in your comments.




About

Eric Goodwin is a web developer living in Victoria BC, Canada. You can contact him at eric@ericgoodwin.com

Open Source

Projects

Elsewhere

Archives