Tell me more ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

On my linux machine I have autocomplete for branches with git.

For example I can type git checkout [TAB] and get a list of branches.

Or I can type git checkout feb* [TAB] to get all branches that begin with "feb".

How can I get this functionality on a Mac?

I tried downloading and running bash-completion/bash_completion and bash-completion/bash_completion.sh.in but no joy. The first gave error messages about declare: -A invalid_option. The second gave no errors. When I git checkout [TAB] though I am still getting a list of files in the directory, not branches. I am in the project root.

Auto-complete for directories and bash commands are working ok, so it's specific to git.

share|improve this question
Auto completion is provided by the shell (bash in this case). Can't you just copy the autocompletion settingfs from your Linux box onto your Mac? – patrix Jul 8 '12 at 18:37
sure, what and where are those settings that I have never had to look at in Linux ? – Michael Durrant Jul 8 '12 at 18:38
2  
Look for any complete statements in your .bashrc (or .bash_profile) on the Linux box. As for the downloaded files, where did you download them from? – patrix Jul 8 '12 at 18:46
Added more info, specifically: " Auto-complete for directories and bash commands are working ok, so it's specific to git. " – Michael Durrant Jul 8 '12 at 20:45
my linux .bashrc's don;t have it. – Michael Durrant Jul 8 '12 at 20:49
show 1 more comment

2 Answers

up vote 31 down vote accepted

ok, so I needed the git autocompletion script.

I got that from this url:

curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

No need to worry about what directory you're in when you run this as your home directory(~) is used with the target.

Then I added to my ~/.bash_profile file the following 'execute if it exists' code:

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
share|improve this answer

You could install bash and git from MacPorts with the "bash_completion" option enabled. It is probably the easiest way of maintaining both git and the completions up-to-date.

http://denis.tumblr.com/post/71390665/adding-bash-completion-for-git-on-mac-os-x-snow-leopard

To install MacPorts: http://www.macports.org/install.php/

Additional info

As for locating the completion settings on your Linux system, have a look in /etc/bash_completion.d/git (it is a long file). Copying that by itself probably won't work, though, as it is designed to be sourced by the completion framework through /etc/bash_completion (which is sourced from /etc/profile.d/bash_completion.sh; files in /etc/profile.d are sourced from /etc/profile).

share|improve this answer
macports is old and I am using homebrew. I've posted my answer above for others. – – Michael Durrant Nov 16 '12 at 18:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.