Flo
Flo

Reputation: 167

Automatic display of git status in Linux bash

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.

Upvotes: 13

Views: 17805

Answers (2)

ofaurax
ofaurax

Reputation: 1576

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)")\$ '

Upvotes: 18

gauteh
gauteh

Reputation: 17215

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)")\$ '

Upvotes: 9

Related Questions