When I work in a git repository directory that has many branches, I want to know which branch I am currently in on my shell prompt. Below is the sample output result.
Before my shell prompt is like this. Note suppose I am on folder ~/general/repos/playground
.
sony@ubuntu:~/general/repos/playground$
We will create like this.
sony@ubuntu:~/general/repos/playground (master)$
On above example, I am on my playground
git repository directory and it display the branch name master
. It means I am on master
branch now.
Today I will share how to create it.
Scenario
I want to add git branch name to my shell prompt when I am in a project repository directory. I am using Ubuntu, so the default shell is bash
.
Step-by-step to go there
Step 1. Go to your shell prompt first.
Step 2. We have to know the current prompt setting, type like below.
echo $PS1
$PS1 is the enviroment variable that store the default prompt setting we see every time when we log in the console.
On mine, the setting is like below.
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
Looks great.
Step 3. Open file ~/.bashrc
vi ~/.bashrc
Step 4. Add this function on the bottom of your ~/.bashrc
file.
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
Step 5. Add this on the bottom of your ~/.bashrc
file. Add the git_branch
function on the shell prompt, by adding it on PS1
variable. Sample like below (that based on my $PS1
output above).
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$(git_branch)\$ "
Step 6. You are done. Now save the file ~/.bashrc
and run this command to reflect the changes.
source ~/.bashrc
Or you can close the terminal and re-open again to reflect the changes.
Now on my shell prompt will display like below.
sony@ubuntu:~/general/repos/playground (master)$
Add color to make it better
This is just additional cosmetics things. I will make the branch indicator (master)
to green for foreground color. Add this \[\033[00;32m\]\$(git_branch)\[\033[00m\]
to your PS1
variable. On mine is like below.
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[00;32m\]\$(git_branch)\[\033[00m\]\$ "
That's it.
Thank you and I hope you enjoy it.
Reference
- https://en.wikibooks.org/wiki/Guide_to_Unix/Explanations/Shell_Prompt
- https://www.shellhacks.com/show-git-branch-terminal-command-prompt/
- https://linoxide.com/how-tos/change-bash-prompt-variable-ps1/
- https://askubuntu.com/questions/590899/how-do-i-check-which-shell-i-am-using
Credits
- Cover image is photo by Adrianna Calvo from Pexels at https://www.pexels.com/photo/sea-nature-beach-sand-21859/
Top comments (14)
I want to add a * if there are not staged file changes and a + if there are staged file changes. I used to have that on my old computer and want to do on my new one. Do you have any idea on how I can do it?
Thanks!
Maybe it help you, add these lines to .bashrc :
Thanks
Awesome article. One gotcha though, If I make a new branch from the terminal it doesn't immediately reflect the changes. I have to open a new terminal or run
source ~/.bashrc
againThanks for the comment, yes yes you are right. I update also the article.
Awesome!
Thank you!
Thanks, man! :)
I am adding at the end of the file the git branch function. and how do I get out of the file ~/.bashrc, to continue the tutorial and write the next code on shell prompt?
How to change color for git branch name??? Please! defalut color is white, i wanna change color.
I think on the article already explained :)
It works for me. Thank you!
How to change branch name to other color? (for example: red,purple)
Hi Chien, thanks for reading my article, for changing to other color please refer to color code, for example in this link misc.flogisoft.com/bash/tip_colors... or this shellhacks.com/bash-colors/
Awesome ! Thank you for this nice article !