Show Git Branch on Shell/Terminal in Color

Git Branch Name on Terminal

The default look of the Mac Terminal and Linux Shell is simply black and white. So, we all have wished if it was a bit prettier and easier to read.

Also, it would be nice to see a git branch with the current location. Though you can customize the design of the Mac Terminal, I'd like to share how to customize the shell design on a more basic level.

Bash Config Files

When the Login Shell launches, .profile or .bash_profile would be loaded. Usually, if .bashrc exists in the user root, that gets loaded next.

By the way, "rc" of .bashrc is supposedly mean "run commands."

Differences Between .bash_profile and .bashrc

.profile gets loaded on every login shell.

.bash_profile is loaded when opening the bash of the login shell since it is the user's profile.

.bashrc gets loaded on the non-login shell. So, you can configure global settings on it and load it in .bash_profile to apply the settings.

For Mac

For Mac, .bash_profile exists in the user home.

export PATH=/usr/local/bin:/usr/local/opt/php70/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
export PATH=/usr/local/sbin:/usr/local/opt/php70/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
# ...
if \[ -f ~/.bashrc \]; then
. ~/.bashrc
fi

As you can see, .bashrc is being loaded here if it exists.

Linux - Ubuntu

Ubuntu uses .profile for initialization.

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if \[ -n "$BASH\_VERSION" \]; then
# include .bashrc if it exists
if \[ -f "$HOME/.bashrc" \]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

After the comments, there is the same code to load .bashrc.

As the comment describes, .bash_profile and .bash_login would be prioritized if they exist. So, it would be a good option for overriding some settings.

.bashrc

We can use the same syntax for Mac and Linux. Please create .bashrc if it doesn't exist.

Change the Color and EOL

export PS1='\\\[\\033\[01;32m\\\]\\u@\\h\\\[\\033\[01;33m\\\] \\w \\n\\\[\\033\[01;34m\\\]\\$\\\[\\033\[00m\\\] '

I'm not going into the details here, but you can change the numbers like 32 or 33 to change the colors. The 256 Color scheme is used and go ahead to see the list here.

PS1 is the variable that hold the prompt design.

Show the Git Branch

git_branch() {
git branch 2\> /dev/null | sed -e '/^\[^\*\]/d' -e 's/\* \\(.\*\\)/ (\\1)/'
}
export PS1='\\\[\\033\[01;32m\\\]\\u@\\h\\\[\\033\[01;33m\\\] \\w $(git\_branch) \\n\\\[\\033\[01;34m\\\]\\$\\\[\\033\[00m\\\] '

I got this idea from this gist. The function git_branch holds the code that shows a branch name if it exists.

Please make sure that Git is installed since this would end up in errors otherwise.

References

COPYRIGHT © 2023 Kohei Ando