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.

12 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 ;-)

  7. 08/12/24 - railsie Says:

    git completion is often negleted function. thank you for a reminder!

  8. 09/02/17 - rvr Says:

    i’m having trouble getting this set up. i’m on os x. i didn’t have .bashrc, so i made it. here’s what it has.

    export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
    export ARCHFLAGS=' -arch i386' 
    export DYLD_LIBRARY_PATH=/usr/local/lib/instantclient:$DYLD_LIBRARY_PATH
    export PATH=/usr/local/lib/instantclient:$PATH
    

    in my .bash_profile, i have:

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

    no matter what i’ve tried, i get a syntax error:

    : command not found
    'bash: /Users/river/.git-completion.bash: line 48: syntax error near unexpected token `in
    'bash: /Users/river/.git-completion.bash: line 48: `case "$COMP_WORDBREAKS" in
    -bash: __git_ps1: command not found
    

    any help would be appreciated. it’s really tough to find useful info on this online. thanks.

  9. 09/02/23 - CodeSturgeon Says:

    rvr – I had the same problem. Turns out that the file I downloaded was in DOS format, not UNIX format. The extra (invisible) line ending chars were being taken as commands by bash. I used vi to fix it. Load the file in vi, and issue the following commands: ’:set fileformat=unix’ ’:wq’

  10. 09/03/05 - Geoffrey Grosenbach Says:

    The zsh is a great alternative to bash and includes git completion in the default install.

    I’m not sure if this applies to the bash version, but I’ve found that setting up a shell alias for frequently used commands retains the completions, but an alias configured within git won’t work. So I use things like “gco” instead of “git co” for “git checkout.”

  11. 09/04/09 - Bill Horsman Says:

    Hi Eric,

    Googling around for git completion on mac and stumbled across your site. Got completion working on my Mac now :) Thanks.

    We should grab a beer sometime. The fact that I’m living on Vancouver Island right now is not unconnected with you putting me in touch with Paul Crawford back in July 2008. Thanks for that too!

    Bill

  12. 09/06/23 - Aaron Meurer Says:

    Just a heads up, if you want to change your PS1, you need to use single quotes, not double quotes. Using double quotes, for whatever reason, prevents the PS1 from auto updating when you switch branches or cd in or out of a git directory. At least it is this way on Mac OS X 10.5.

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