Last Updated: August 17, 2020
·
4.45K
· janosgyerik

Enabling Git shell completions in OSX

Hard to believe I survived so long with Git in the shell without setting up auto-completion features in Mac OS X. But that's what happened!
At last, I got annoyed by some longish remote names and some longish branch names, to the point that gave me the right push.

I have an oldish PC, with Snow Leopard and MacPorts.
Based on this article on superuser.com, it seemed pretty simple to do:

sudo port selfupdate
sudo port install git-core +bash_completion

You have to pay attention to the output of these commands.
Somewhere in the middle the install step mentioned that the completion features will only work in Bash version >= 4.1.
Checking what I have at hand:

$ echo $BASH_VERSION 
3.2.48(1)-release
$ echo $SHELL
/bin/bash
$ which bash
/opt/local/bin/bash
$ bash --version
GNU bash, version 4.2.45(2)-release (i386-apple-darwin10.8.0)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

So my current shell is /bin/bash, version 3.2,
but a 4.2 version exists already in my system at /opt/local/bin/bash.
Let's change my shell:

$ chsh -s /opt/local/bin/bash
Changing shell for janos.
Password for janos: 
chsh: /opt/local/bin/bash: non-standard shell

It didn't work, apparently because the shell I specified is "not standard".
According to the man page of chsh,
standard shells are the ones listed in /etc/shells.
This Bash 4 was installed by MacPorts,
and I do mean it as a standard shell,
so let's add it to the list:

echo /opt/local/bin/bash | sudo tee -a /etc/shells 

After this I could change my shell,
and in a new terminal window or tab I could start enjoying auto-completion in Git commands, yeay!
Most importantly in operations with remotes and branches.

13 Responses
Add your response

hi,

i'm using git-sh like https://github.com/thoughtbot/gitsh or https://github.com/rtomayko/git-sh ;-)

maybe there is something for you

over 1 year ago ·

Thanks @shupal! I know about gitsh, but I haven't tried it. I imagine it could be great in projects where you don't need to do much else on the command line than running Git commands. For me that's usually not the case: I run many shell commands between Git operations, so I feel it wouldn't be ergonomic for me. But who knows, maybe!

over 1 year ago ·

ahh okay i see your point.
I handle that via windows arrangement on iterm2. Left top is my git stuff, left bottom sass/npm etc. ;-)

Anymway nice tip!

over 1 year ago ·

Fish shell does this not only for Git but other command line tools with man pages as well.

Once Fish is installed through homebrew or apt-get, just run "fishupdatecompletions", and that is it.

over 1 year ago ·

This path didn't work for me.
Following the guide from the MacPorts project worked however: https://trac.macports.org/wiki/howto/bash-completion

over 1 year ago ·

@grevenx which step didn't work for you? Thanks for the link in any case, that's very informative!

over 1 year ago ·

@janosgyerik no errors, but the autocomplete just didn't work :/ Using OS X 10.9.2

over 1 year ago ·

I use oh-my-zsh does the same and more https://github.com/robbyrussell/oh-my-zsh

over 1 year ago ·

Thanks @ppone for recommending fish! As far as I remember, fish sacrifices some bash functionality in favor of being more user friendly. So it might not be my cup of tea (I'm a huge bash fan and use it extensively), but I should give it a try!

over 1 year ago ·

Thanks @edderrd oh-my-zsh looks interesting! I use bash extensively and it does everything I need perfectly well. But I'm aware of zsh and I should definitely try. Maybe the time is now.

over 1 year ago ·

oh-my-zsh FTW - the theming and plugin architecture alone is well worth the install. It also auto-updates and has a very large community. For workflow it's the best choice.

over 1 year ago ·

Though I also use oh-my-zsh (it's awesome), I don't think you need an oh-my-zsh plugin (e.g., gitfast) to do git completion in zsh. Zsh provides a default git completion with some zsh-specific features, and I'm pretty happy with that one.

According to the gitfast plugin:

(The plugin) enables the zsh completion from git.git folks, which is much faster than the official one from zsh. A lot of zsh-specific features are not supported, like descriptions for every argument, but everything the bash completion has, this one does two (as it is using it behind the scenes). Not only is it faster, it should be more robust, and updated regularly to the latest git upstream version.

over 1 year ago ·

OMG ! chsh was the only one command that was missing for me. NOTE: changing Terminal.app default shell is no sufficient, you MUST do the chsh as stated here.

over 1 year ago ·