Improving your Git Experience in Bash

AuthorMáximo Mussini
·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 💻

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/" ]] && source "$(brew --prefix)/opt/bash-git-prompt/share/"

Installation on Ubuntu

It might be necessary to clone the project's git repo, and source 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 ~/ 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 easier for me to understand the current git status 😅

Emoji Theme


override_git_prompt_colors() {

  PathShort="\W" # Display only the current folder

  # Display the current folder first

  # Skip the default prefix

  # Use whitespace as separator

  # Skip remote branch

  # Use yellow for the current branch

  # Use red and green for behind and ahead origin

  # Add a few emojis to make it fun!
  GIT_PROMPT_STAGED="${Yellow}👍 "
  GIT_PROMPT_CHANGED="${Yellow}✏️ "
  GIT_PROMPT_STASHED="${Yellow}📦 " # A lot nicer than the default flag
  GIT_PROMPT_CLEAN="${ResetColor}✅ "  
  GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING=" 🔒 " # when a branch is untracked

  # Skip the default suffix

  # Skip the default ending

reload_git_prompt_colors "Custom"

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

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:


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 😎