If you're a developer or someone who works with Git regularly, you may have wished for a more organized and informative terminal prompt. Specifically, it can be incredibly useful to always know which Git branch you're on directly from the terminal. I’ll show you how to do it.
Before: The Default Terminal Prompt
As you can see, the prompt was simple. It displayed my username, hostname, and the current directory.
The Goal
Table of Contents
- Accessing the ~/.bashrc file
- Adding Git Branch Information
- Customizing the Prompt
- Final Step: Apply Changes
- Result
- Other shells (zsh, fish)
Step-by-Step: How I did it
To achieve this, I modified my terminal prompt by customizing the PS1
environment variable. The PS1
variable defines the prompt that appears in your terminal. By customizing it, you can add dynamic content, such as the current branch name.
Accessing the ~/.bashrc file
The PS1
variable is stored in the ~/.bashrc
file, which is loaded whenever a new terminal session starts. To modify it, open the file in your preferred text editor:
nano ~/.bashrc
Adding Git Branch Information
I wanted to display the current Git branch only when I'm inside a Git repository. This can be done by using a shell function like parse_git_branch()
which checks for the existence of a .git
directory. If it exists, it extracts the current branch name.
Here's the function I used in my .bashrc
:
parse_git_branch() {
git branch 2>/dev/null | sed -n '/\* /s///p'
}
This function checks the output of git branch
, and extracts the branch name by looking for the line that starts with * (which represents the current branch).
Customizing the Prompt
Next, I customized my PS1
variable to include the current branch. I also added color for each part of the prompt. Here's the code I ended up using:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;91m\]\u@\h\[\033[00m\]:\[\033[01;35m\]\w\[\033[00m\]\[\033[01;92m\]$([[ -d .git ]] && echo " ($(parse_git_branch))")\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$([[ -d .git ]] && echo " ($(parse_git_branch))")\$ '
fi
Explanation:
-
\[\033[01;91m\]:
Sets the username (\u) and hostname (\h) in bright red. -
\[\033[01;35m\]:
Sets the working directory (\w) in light purple. -
\[\033[01;92m\]:
Sets the Git branch name in bright green (which makes it stand out).
With this configuration, the terminal prompt will display the Git branch only when you're inside a Git repository, and it will be highlighted in the bright green color of your choice.
Final Step: Apply Changes
After modifying the ~/.bashrc
file, make sure to apply the changes by running:
source ~/.bashrc
Result
Here’s the result after all the changes were made:
Other shells(zsh, fish)
For Zsh (Z Shell)
Include this in your ~/.zshrc
file:
PROMPT='%F{red}%n@%m%f:%F{magenta}%~%f$([ -d .git ] && echo " (%F{green}$(git rev-parse --abbrev-ref HEAD)%f)") % '
For Fish Shell
Include this in your ~/.config/fish/config.fish
file:
function fish_prompt
set_color red
echo -n (whoami) "@" (hostname) " "
set_color magenta
echo -n (pwd) " "
if test -d .git
set_color green
echo -n "(" (git rev-parse --abbrev-ref HEAD) ") "
end
set_color normal
echo -n "> "
end
I hope this was helpful. If you'd like to try out some other custom colors, feel free to leave a comment below, and I'll be happy to provide you with the code snippet! 💬
Example of the comment
Hey, Nikola! I want:
nickname/hostname color → #FF5733 (Bright Red)
path color → #8E44AD (Purple)
branch color → #2ECC71 (Green)
I promise I will send you the solution within 6 hours. 😊
Top comments (0)