I have been using Ubuntu for most of my development works for a long time. But recently I also started working on a Mac on one of my projects. This was the first time I was using it. Most of the things felt homely but the first thing that caught my attention was, there no auto-complete feature available on the terminal for Git commands.
Git is the first step to start working on any project, so this small thing was what I wanted to fix on Mac. I asked few of my fellow Mac using friends but they said it doesn't work on Mac. So I packed my bag and started sailing the internet on a quest to find a solution to the problem.
I was lucky to not have to go very far. I would like to share how its done and make other's sailing even shorter with this article.
The first thing we need is this script which has all the commands that let Mac understand how to auto-complete git commands.
To be more precise above file contains auto-completion for following git commands
- local and remote branch names :
most essential auto-completion command. As we have long branch names and it is really hard to remember them everytime we have to switch.
- .git/remotes file names and file paths within current working directory and index :
this too is equally important as we have many folders inside folders and files. So this helps auto-complete file names.
- local and remote tag names :
Git has the ability to tag specific points in history as being important. Typically people use this functionality to mark release points (v1.0, and so on).
- git 'subcommands' :
it helps auto-complete git commands like
- git email aliases for git-send-email :
Takes the patches given on the command line and emails them out. Patches can be specified as files, directories (which will send all files in the directory), or directly as a revision list.
You need to save this to the home directory. You can use the following command to download and save this file to said this directory.
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash`
Once this is done, you then need to add this script to your bashprofile file. Like me if, even you are wondering what is bashprofile file and why do we not have the bashrc file like we did in Ubuntu. So I found out
This file is loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command line interface.
We also have
bashrc file on Mac but the difference is it is executed for interactive non-login shells. To know
more click here
Okay, so let's add that file to our
bash_profile so that it is available to our terminal. I'll use nano to do that
nano ~/.bash_profile (use
sudo if required). Add following line at the end of the file
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash fi
control+x and then
y and hit
enter to save the changes. Restart the terminal.
I think you must be able to have all the auto-complete features available on your Mac at this point. Hope it helped and Happy coding.