Improving your Git Experience in Bash

December 14, 2015 4 min read

Bash is the default terminal for most Unix distributions, so it’s very appealing to get the best out of it. If you enjoy customizing your setup, you might want to try something like zsh instead, which comes with similar functionality out of the box.

Let’s take a look at how we can improve our Git usage in bash by adding a few plugins :neckbeard:

Bash Git Autocompletion

A great way to get Git autocompletion is to install the bash-completion package. This plugin will improve our experience when switching or pulling different branches, and is a real time-saver when using topic branches.

Checking out Branches

Installation on OS X

First, install bash-completion using Homebrew:

brew install bash-completion

and then add this line to ~/.bash_profile to load the plugin:

[[ -f "$(brew --prefix)/etc/bash_completion" ]] && source "$(brew --prefix)/etc/bash_completion"

Installation on Ubuntu

sudo apt-get install bash-completion

Bash Git Prompt

Running a Git command without knowing the current branch is like running rm or mkdir in the terminal without knowning the current directory: it’s dangerous and error-prone.

Sure, we could type pwd every time before running those commands, but it wouldn’t be practical. Why not take the same approach for Git, and display the current branch in the terminal prompt?

bash-git-prompt is a project that takes care of that, displaying the current branch and other helpful stats. It has many different themes available, which can be customized by specifying a theme through the GIT_PROMPT_THEME environment variable.

Default Theme

Installation on OS X

First, install bash-git-prompt using Homebrew:

brew install bash-git-prompt

And then source the file in your ~/.bash_profile as follows:

[[ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]] && source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"

Installation on Ubuntu

It might be necessary to clone the project’s git repo, and source gitprompt.sh into the ~/.bashrc file.

Basic Customization

The following are some settings I modified in my ~/.bash_profile:

GIT_PROMPT_ONLY_IN_REPO=1 # Use the default prompt when not in a git repo.
GIT_PROMPT_FETCH_REMOTE_STATUS=0 # Avoid fetching remote status
GIT_PROMPT_SHOW_UPSTREAM=0 # Don't display upstream tracking branch
GIT_SHOW_UNTRACKED_FILES=no # Don't count untracked files (no, normal, all)

These constants must be defined before sourcing bash-git-prompt in the ~/.bash_profile or ~/.bashrc file.

Customizing the Git Prompt

The themes that come bundled with bash-git-prompt displayed too much information for my taste, and the symbols in the default theme are not particularly meaningful.

After trying different themes, I decided to bake my own. I wanted something that emphasized the important bits and pieces, looked clean, and was easy to understand. Fortunately, the plugin provides a command to generate a custom theme file:

git_prompt_make_custom_theme Default

The command creates a ~/.git-prompt-colors.sh based on the default theme and will get loaded by the plugin automatically, so you can start playing with the variables right away. However, you may need to dive into the source code to figure out how each variable is used, and find the ones you want to customize.

After tweaking the variables for a while, I was able to create a custom theme. The expresiveness of emojis made it a lot easier for me to understand the current git status :sweat_smile:

Emoji Theme

Emojis are a great way to make ye olde terminal a bit more fun! :tada:


It would be awesome if bash-git-prompt provided a way to customize the full prompt order and displayed elements, with a template string like:

GIT_PROMPT="{PathShort}{Branch}{Remote}{Modified}{Staged}{Clean}"

That would make it possible to create very different themes, as well as opting out of certain information—like git stashes.

For now, I’m pleased with the result; it’s practical, looks nice, and I don’t need to type git branch to check the current branch :sunglasses:

By @Maximo @MaximoMussini