9

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.

Flo
  • 127
  • 1
  • 1
  • 7
  • 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. – jonrsharpe May 29 '17 at 07: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 [su] *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. – Lasse V. Karlsen May 29 '17 at 07:58
  • @LasseV.Karlsen Ah, I did not know SuperUser so far. Can you just move the question? – Flo May 30 '17 at 08:29
  • 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) – djvg Jun 30 '21 at 08:42

3 Answers3

12

TL;DR on Ubuntu 20.04.1

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)")\$ '
ofaurax
  • 1,417
  • 1
  • 20
  • 27
  • 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! – 8koi Jan 22 '23 at 20:51
8

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)")\$ '
gauteh
  • 16,435
  • 4
  • 30
  • 34
  • 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. – Flo May 30 '17 at 08:14
  • 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. – Flo May 30 '17 at 08:17
  • @DaRealHonk: Google customizing the bash prompt, you can put other stuff into the PS1 variable as well. – gauteh May 30 '17 at 08:45
  • 1
    where is `git-prompt.sh` file? – alper Aug 07 '20 at 16:33
  • how can i keep coloring as well for the folders? – alper Aug 07 '20 at 16:36
  • @alper You can get git-prompt.sh from the git repository https://github.com/git/git/tree/master/contrib/completion – Michael Apr 10 '21 at 10:05
  • @Michael Can this also applied for zsh? – alper Apr 10 '21 at 11:44
  • I'm pretty sure you can given that it's still bash. – Michael Apr 10 '21 at 12:22
  • Not sure but I keep getting following error in bash and zsh: `bash: bin/git-prompt.sh: line 320: syntax error near unexpected token ] bash: bin/git-prompt.sh: line 320: \` p[\ \ ]|pick[\ \ ]*)'` – alper Apr 10 '21 at 12:43
-2

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.

  • No, that is not what I meant. I was hoping to get more information about the current git repo, not only add colors to what I already had. But thanks anyway, maybe I will play a bit with coloring my bash after using the solution by @gauteh. – Flo May 30 '17 at 08:20