On Git Bash on Windows I get nice coloring and the current branch is shown, like this:
How can I get the same coloring and prompt on Linux? On Linux I use the regular terminal, which doesn't show the current branch.
Next is current git branch ( \$ (parse_git_branch)) if we are in a git repo, in light red ( [91m) color. The last part of the prompt is symbol $ in default ( [00m) color.
You can permanently set up your bash output to show your git-branch name. It is very handy when you work with different branches, no need to type $ git status all the time. Github repo git-aware-prompt . mkdir ~/.bash cd ~/.bash git clone git://github.com/jimeh/git-aware-prompt.git
So adding a way to display the active git branch can help developers avoid the branching problems and code conflict with other developers some time. In this post, you will get a chance to learn how you can show your active git branch on your Linux terminal on the bash prompt. Open up your terminal and hit cd ~ to stay on the root.
As long as you're inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes. This should be the accepted answer, as it's clear, concise, and does the job, and it works on other platforms too.
If you would like to retain the same coloring of your existing linux terminal and also show the current git branch, you can add the following to your default PS1.
PS1 basically tells your bash prompt what to display. Ref: How to Change / Set up bash custom prompt (PS1) in Linux
I am using Ubuntu 20.04 and the default PS1 is found under if [ "$color_prompt" = yes ]; then
in the ~/.bashrc file.
Steps:
if [ "$color_prompt" = yes ]; then
#show git branch
show_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi
The entire thing should look something like this:
#show git branch
show_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi
It should looks something like this: Terminal with git branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With