Adding Git Autocomplete to Bash on OS X


Git Logo
If you use git on the command line a lot then git bash autocomplete is an amazing tool to have in your toolbelt. Unfortunately, just by installing git you don’t get to use it. You need to add command line functionality to your environment. The easiest way to add autocomplete is by downloading a file and sourcing it in your `~/.bash_profile`.

Download Script

The bash script is in the contrib part of the git repository. You need to download it where you want it to go, I just put it in the home directory.

Source Scripts

Now you need to source the script to add it to your environment. Add the following to your ~/.bash_profile.
1
2
3
if [ -f ~/git-completion.bash ]; then
  . ~/git-completion.bash
fi
This simply checks if the bash file is there and if so it executes it which adds it to our environment. Now do `source ~/.bash_profile` and type in `git b<tab>` to see the auto-complete starting to work.

Conclusion

Now you should be able to type in `git checkout t` then it will auto fill in the rest of the branch name that starts with `t` when you hit tab. The sub-commands will also auto-complete as well as many other things. I find this particularly nice since we have some long/tough branch names so auto-complete makes switching branches quite easy and quick.
Previous
Next Post »