Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show current branch and coloring on Linux (like Git Bash on Windows)

On Git Bash on Windows I get nice coloring and the current branch is shown, like this:

enter image description here

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.

like image 436
sashoalm Avatar asked Feb 13 '17 16:02

sashoalm


People also ask

What is the color of nextnext command in Git?

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.

How to show Your git-branch name in Bash?

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

How to show active git branch in Linux terminal?

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.

How do I see uncommitted changes in a git branch?

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.


1 Answers

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:

  1. Open the ~/.bashrc file and find if [ "$color_prompt" = yes ]; then
  2. Define the following bash function (Thanks @Nogoseke) above the if statement (You can define it anywhere above the PS1 value in the file).
#show git branch
show_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
  1. Update the PS1 values to the following:
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
  1. Save the changes and restart your terminal.

It should looks something like this: Terminal with git branch

like image 125
Arpan Roy Avatar answered Sep 19 '22 21:09

Arpan Roy