Contact Us
Thought Leadership

Nice Git Prompt in Bash

December 9, 2019

If you work with git using the command line interface (CLI), you can configure your prompt to provide very useful information on real time about the status of the git repository in the current working directory; for example, the branch name, local and remote branch delta, number of new, modified, files, etc. The following configuration is focused on Bash and should work for almost any operating system with Bash support (Linux, MACOS, Windows with Cygwin, etc.).

Configuration

  1. Download or clone magicmonty/bash-git-prompt in ~/.bash-git-prompt:
    git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt
  2. Add the following lines to ~/.bashrc:
GIT_PROMPT_ONLY_IN_REPO=1
GIT_PROMPT_THEME=Custom
GIT_PROMPT_PREFIX="("
GIT_PROMPT_SUFFIX=")"
GIT_PROMPT_START="[e[1;33m][u@h W[e[m]"
GIT_PROMPT_END="[e[1;33m]]$[e[m] "
source ~/.bash-git-prompt/gitprompt.sh

… and that’s all!

Prompt structure

The configuration above will modify the prompt when your current working directory is a git repository: [user@hostname <cwd> (<branch>|<status>)] Examples: (master|✔) : repository clean. (master↑3|✚1) : on branch master, ahead of remote by 3 commits, 1 file changed but not staged. (status|●2) : on branch status, 2 files staged. (master|✚7…) : on branch master, 7 files changed, some files untracked. (master|✖2✚3) : on branch master, 2 conflicts, 3 files changed. (master|⚑2) : on branch master, 2 stash entries. (experimental↓2↑3|✔) : on branch experimental; your branch has diverged by 3 commits, remote by 2 commits; the repository is otherwise clean. (:70c2952|✔) : not on any branch; parent commit has hash 70c2952; the repository is otherwise clean

Local status symbols

 : repository clean ●n : there are n staged files ✖n : there are n files with merge conflicts ✖-n : there are n staged files waiting for removal ✚n : there are n changed but unstaged files …n : there are n untracked files ⚑n : there are n stash entries

Branch Tracking Symbols

↑n : ahead of remote by n commits ↓n : behind remote by n commits ↓m↑n : branches diverged, other by m commits, yours by n commits L : local branch, not remotely tracked

A real example

Notice how the prompt changed according to the status:

Bonus: Regular Bash prompt

When I work at the console I like to things:

  1. Clearly identify if I am working as root besides the hash sign (#) vs the dollar sign ($).
  2. Identify visually the output of the last command.

To accomplish the 2 requests above I add the following lines to /etc/profile.d/ps1.sh:

if [ $(id -u) -eq 0 ]; then
    export PS1="[e[38;5;166m][u@h W]#[e[m] "
else
    export PS1="[e[1;33m][u@h W]$[e[m] "
fi

Notice how the color of the prompt changes when I am regular user or root:

Related Insights

We are results-driven with a focus on providing customer service excellence

IO Connect Services is here to help you by offering high-quality cloud technology solutions.
Connect with us
®2024 IO Connect Services
Privacy PolicyCookie Policy
magnifiercrossmenuchevron-down
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram