Configuration
- Download or clone magicmonty/bash-git-prompt in ~/.bash-git-prompt:
git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt
- 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:
- Clearly identify if I am working as root besides the hash sign (#) vs the dollar sign ($).
- 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: