Automatic display of git status in Linux bash

13.2k views Asked by At

I started working with git in a Windows system. I used the Git Shell that comes with Git Desktop. This commandline tool always displays the branch you are currently in and a short, colored form of git status (# of untracked files, # of changed files, # of deleted files). I found this really convenient.

Now I changed my system completely to Linux and I did not find anything similar. Is there a way to teach the Linux bash displaying the branch and status just like the Windows Git Shell does? I am currently working with the Xubuntu (16.04) Terminal.

3

There are 3 answers

9
gauteh On BEST ANSWER

According to the GIT-SCM book, and assuming you are using bash, you can use the git-prompt.sh script provided by either git or some other package manager in your distro.

. ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\w$(__git_ps1 " (%s)")\$ '
1
Just a HK developer On

I know what you mean. In Windows, after you install Git, you will get Git Bash Here. When you launch this, a terminal will come up and the prompt will have color. [The prompt that I see is: username@machine MINGW32 /C/Working/GitTutorial (master). username@machine is in green. MINGW32 is in purple. Path is in yellow. Branch is in blue.]

Now if your system is Unix/Linux, yes, when you open a terminal, you can make the color to have color. Just Google and find 'bash prompt', I get https://wiki.archlinux.org/index.php/Bash/Prompt_customization

I am sure with a bit more searching, you can make the prompt in Unix/Linux to look exactly what you are asking.

2
ofaurax On

TL;DR on Ubuntu 20.04.1 and later

Add this to your ~/.bashrc:

source /etc/bash_completion.d/git-prompt
export GIT_PS1_SHOWDIRTYSTATE=1
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_ps1 "(%s)")\$ '

Details

This answer is for people who wants to enhance the original prompt provided with Ubuntu 20.04.1.

The shell is provided by the git-core package

source /etc/bash_completion.d/git-prompt

export GIT_PS1_SHOWDIRTYSTATE=1 is needed to display when current state is modified.

The initial prompt is:

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\]\$ '

So you can enhance it that way by adding the git state at the end:

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_ps1 "(%s)")\$ '