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/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 easier for me to understand the current git status 😅

Emoji Theme

# .git-prompt-colors.sh

override_git_prompt_colors() {
  GIT_PROMPT_THEME_NAME="Custom"

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

  # Display the current folder first
  GIT_PROMPT_START_USER="${Green}${PathShort}"
  GIT_PROMPT_START_ROOT="${Green}${PathShort}"

  # Skip the default prefix
  GIT_PROMPT_PREFIX="${ResetColor}"

  # Use whitespace as separator
  GIT_PROMPT_SEPARATOR=" "

  # Skip remote branch
  GIT_PROMPT_REMOTE="${ResetColor}"
  GIT_PROMPT_UPSTREAM="${ResetColor}"

  # Use yellow for the current branch
  GIT_PROMPT_BRANCH="${Yellow}"

  # Use red and green for behind and ahead origin
  GIT_PROMPT_SYMBOLS_BEHIND="${Red} ↓"
  GIT_PROMPT_SYMBOLS_AHEAD="${Green} ↑"

  # Add a few emojis to make it fun!
  GIT_PROMPT_STAGED="${Yellow}👍 "
  GIT_PROMPT_CONFLICTS="${Red}❌ "
  GIT_PROMPT_CHANGED="${Yellow}✏️ "
  GIT_PROMPT_UNTRACKED="❔ "
  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
  GIT_PROMPT_SUFFIX=" "

  # Skip the default ending
  GIT_PROMPT_END_USER="${ResetColor}"
  GIT_PROMPT_END_ROOT="${ResetColor}"
}

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:

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 😎