Making the Linux Shell Prompt more Git Friendly

Every time I set up a new Linux machine for development, I frequently find myself always Googling the Bash shell modifications to make the shell prompt more Git friendly. Never again!

Git Branch Autocomplete

  1. Download this file https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-completion.bash to ~/.git-completion.bash by running the following commands:
    1. Go to your home directory cd ~
    2. Download the file wget https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-completion.bash
    3. Hide the file mv git-completion.bash .git-completion.bash
  2. Create a reference to the git-completion bash script on your .bashrc file by running echo -e "\nsource ~/.git-completion.bash" >> ~/.bashrc
  3. Load the updated changes to your ~/.bashrc file by running source ~/.bashrc

Show Git Branch on the Shell Prompt

  1. Download this file https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh to ~/.git-prompt.sh by running the following commands:
    1. Go to your home directory cd ~
    2. Download the file wget https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh
    3. Hide the file mv git-prompt.sh .git-prompt.sh
  2. Create a reference to the git-prompt shell script on your .bashrc file by running the command echo -e "\n. ~/.git-prompt.sh" >> ~/.bashrc
  3. Modify the existing PS1= value in your ~/.bashrc file or inject an override of the PS1 variable.
    1. To inject an override, run the command echo "export PS1=\"\\[\\e[32m\\]\\u@\\h\\[\\e[00m\\]:\\[\\e[94m\\]\\w \\[\e[91m\\]\\\$(__git_ps1 \\\"(%s)\\\")\\[\\e[00m\\]\$ \"" >> ~/.bashrc
    2. This will include the following line to your ~/.bashrc file:

      export PS1="\[\e[32m\]\u@\h\[\e[00m\]:\[\e[94m\]\w \[\e[91m\]\$(__git_ps1 \"(%s)\")\[\e[00m\]$ "
      

      This can be further customized to be formatted and output more information by adding additional parameters. (See below)

  4. Load the updated changes to your ~/.bashrc file by running source ~/.bashrc

Formatting the Prompt

Colours can be set by adding additional values to the PS1 string in \e[32m where 32 represents one of the color codes below. This code will override the color for the rest of the prompt so if you want to change the color to something else or reset it, you will need to include either another color code or the reset code \e[0m] at the point in the string where you want to end the current color.

ColorForeground CodeBackground Code
Black3040
Red3141
Green3242
Yellow3343
Blue3444
Magenta3545
Cyan3646
Light Gray3747
Gray90100
Light Red91101
Light Green92102
Light Yellow93103
Light Blue94104
Light Magenta95105
Light Cyan96106
White97107

Additional formatting options include:

CodeDescription
0Reset/Normal
1Bold text
2Faint text
3Italics
4Underlined text

Adding Additional Information to the Prompt

Additional parameters can be added to PS1 to include a variety of information such at the current time.

d - the date in "Weekday Month Date" format (e.g., "Tue May 26")
e - an ASCII escape character (033)
h - the hostname up to the first .
H - the full hostname
j - the number of jobs currently run in background
l - the basename of the shells terminal device name
n - newline
r - carriage return
s - the name of the shell, the basename of $0 (the portion following the final slash)
t - the current time in 24-hour HH:MM:SS format
T - the current time in 12-hour HH:MM:SS format
@ - the current time in 12-hour am/pm format
A - the current time in 24-hour HH:MM format
u - the username of the current user
v - the version of bash (e.g., 4.00)
V - the release of bash, version + patch level (e.g., 4.00.0)
w - Complete path of current working directory
W - the basename of the current working directory
! - the history number of this command
# - the command number of this command
$ - if the effective UID is 0, a #, otherwise a $
nnn - the character corresponding to the octal number nnn
\ - a backslash
[ - begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
] - end a sequence of non-printing characters
Tags