13

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.

4
  • Bash-it adds repo status to your shell prompt, there are probably other tools that do the same. But tool recommendations are off-topic here. Commented May 29, 2017 at 7:54
  • If your question is what other shell or tool you need to get or install in order to get this status, then this question is off-topic. If your question is how to configure bash to show this then Super User might be a better place. Since it involves git, it might also be a good fit here. Right now the question is a bit diffuse. Commented May 29, 2017 at 7:58
  • @LasseV.Karlsen Ah, I did not know SuperUser so far. Can you just move the question? Commented May 30, 2017 at 8:29
  • 1
    Instructions can be found in the git-sh-prompt file, e.g. less /usr/lib/git-core/git-sh-prompt (at least on ubuntu 20.04) Commented Jun 30, 2021 at 8:42

2 Answers 2

18

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)")\$ '
Sign up to request clarification or add additional context in comments.

3 Comments

This dis the trick! and if you don't care about the user@username: stuff you can just remove everything before that exact : , like so export PS1='\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 "(%s)")\$ ' This response should be the selected!
I do get "__git_ps1(%s): command not found".
For me, it appears it's defined by /etc/bash_completion.d/git-prompt which calls /usr/lib/git-core/git-sh-prompt
9

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

9 Comments

Thanks, that did at least part of the trick. Now, the current branch is displayed. I saw that this can be customized further, as soon as I found the way to reproduce the information in Windows (color or no), I will post it here.
There is a minor drawback: Before adding the code you posted above to the '.bashrc', the bash always showed "USERNAME@COMPUTER:~$" in the beginning of each line. Afterwards it only shows "~$". Fine for me, though I do not understand why this happened exactly.
@DaRealHonk: Google customizing the bash prompt, you can put other stuff into the PS1 variable as well.
where is git-prompt.sh file?
how can i keep coloring as well for the folders?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.