Git Autocomplete setup

Autocomplete in almost every known shell is spoiled me, which is why I’d expect it to do the same for git. Sadly, it doesn’t come packaged with the default git installation on Mac OS X (El Capitan).

1. Download the git-completion.bash script and store it a known location

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 57021  100 57021    0     0  31591      0  0:00:01  0:00:01 --:--:-- 31590

2. Add the script to your .profile

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

3. You are done.

Hitting tab after git command now shows

$git 
add                  bundle               config               gc                   log                  push                 reset                stash                wtf 
am                   checkout             describe             get-tar-commit-id    merge                rebase               revert               status               
annotate             cherry               diff                 grep                 mergetool            reflog               rm                   submodule            
apply                cherry-pick          difftool             help                 mv                   relink               send-email           subtree              
archive              citool               fetch                imap-send            name-rev             remote               shortlog             svn                  
bisect               clean                filter-branch        init                 notes                repack               show                 tag                  
blame                clone                format-patch         instaweb             p4                   replace              show-branch          verify-commit        
branch               commit               fsck                 interpret-trailers   pull                 request-pull         stage                whatchanged   

Making your scrips chkconfig aware

If you want to have your scripts run at startup and shutdown, there is a specific way of doing it using chkconfig (or /sbin/chkconfig).

The first thing to do is to get to know about runlevels. Run levels informally define the state to which your system is booting up to. Runlevel 5 in Fedora/RedHat/CentOS is the default and means Multi-user with X. Runlevel 1 typically means single user mode, Runlevel 3 is Multi-user mode without X. There are 7 runlevels, 0 through 6. The file /etc/inittab tells you the run level that your system boots up to by default.

Firstly you need to know the runlevels in which you need to run your startup scripts. Typically you’d be running your scripts in runlevel 3,4,5.

The first thing you need to add to your script is the comment

#chkconfig 345 98 02
#description: This is what my script does.

–The first set of numbers after chkconfig are the runlevels you want your script to run at startup.
–The second number is the priority of the script at start time i.e. 98 in this case. It means that your script will run after all scripts with priority less than 98 have already run.
–The third number is the priority of the script at shutdown i.e. 02 in this case.

When you add your script using the command chkconfig –add , 7 symlinks are created. Firstly the symlinks prefixed with S are placed in /etc/rc.d where runlevel are the levels you specified that your script should run at startup. Then in the remaining /etc/rc.d, symlinks prefixed with K are created.