Enhance Your Shell With Git For Bash

Git in bash, install bash features for Git

If you are a regular command line user you are probably familiar with the frustration of slamming your TAB key and receiving no auto completions for a long command. In many cases, it’s because those auto completions are simply not present in your Bash configuration. In this article, you’ll learn how to add auto completions for Git as well as enhance your prompt with Git information like branch names and status indicators.

Check Your Distribution

The Git features for Bash discussed in this article may be included in your distribution by default. You can check this by trying to do a TAB auto completion on a git command like git checkout:

git chec<TAB>

If the command does not complete itself, you can proceed with the rest of the article to get the Git bash completions installed.

Likewise, you will also want to make sure you have Git and wget installed in your system.

Download the Git Completions File For Bash

In order to provide your Bash installation with the Git completions you will need to source the git-completion.bash file and load it before every shell session. This file should be available in your Git source code, but it’s easier to simply download it from the Git repository at GitHub:

wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

You can save this file to any location in your file system that you prefer, but for the sake of simplicity the default home directory for your user is a good place to start — and will be used below when sourcing this file in your Bash configuration.

Download Git Prompt For Bash

As in the above download, we will retrieve the Git prompt file from the repository at GitHub:

wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

Edit the .bashrc File

Add the following text into your .bashrc file:

# Git completions
. ~/git-completion.bash

# Git prompt
. ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\w$(__git_ps1 " (%s)")\$ '

When you reload a new shell, your prompt should look like this when inside a Git project directory:

~/git-directory (master #)$   

The first part is the name of the directory you are visiting, and in the parentheses you have the branch name and a status indicator followed by the beginning of your prompt ($).

The hash mark (#) after the branch name will change based on the status of the project. For example, if you have added modifications to the staging index you will see a + sign. If there are unstaged changes you will see a *. If the working directory is clean you will see no mark by the branch name.

For more information on different options and configurations of your Git prompt you can read the documentation and comments provided in the git-prompt.sh file.

Well done! You now know how to expand your Bash with helpful Git information. What’s the next step on your Git journey?

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X