21

How can I configure the terminal to display the current git branch?

I'd like to see the second line rather than the first:

andy@bob:~/my_projects/project_x$ 
(master)~/my_projects/project_x$

I don't want to have to run git status to see which branch I am currently on anymore!

4
  • Make sure to include whole bash prompt in double quotes. If you put it in single quotes, the value inside parenthesis won't evaluate.
    – superjisan
    Mar 11, 2014 at 5:40
  • 1
    @user3173748 If you want something in the prompt to be evaluated on usage time, it has to be single quoted or at least escaped or it will be evaluated on definition. That aside, there isn't even anything in the question to be evaluated as it just shows, how the prompt should look like and not how it is defined.
    – Adaephon
    Mar 11, 2014 at 7:26
  • I know duplicates don't have to be older, but it amuses me this 2013 question is closed as duplicate of a 2016 one.
    – hayd
    Nov 8, 2017 at 6:38
  • 1
    Here's what the git book has to say about it: "... copy the contrib/completion/git-prompt.sh file from Git’s source repository ..." which would be here. Further step-by-step instructions are found in the file itself.
    – djvg
    Oct 23, 2018 at 12:39

4 Answers 4

34

You can add the following code to you .bashrc file:

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$(parse_git_branch) $ "

You can move around these component parts to configure to your tastes, for example to prepend $(parse_git_branch) and not show the user@computer part I used:

PS1="\$(parse_git_branch)${debian_chroot:+($debian_chroot)}\w$ "

Which displays:

(master)~/my_projects/project_x$ 

See also: What is this PS1 variable doing in .bash_profile file?

5
  • Is it possible to avoid the brackets and change the color? Jun 18, 2013 at 15:33
  • 1
    @CedricReichenbach yes, to avoid brackets you just rewrite the PS1 line (without the brackets). For color see here: tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
    – hayd
    Sep 11, 2013 at 23:24
  • Well, I rewrote the parse_git_branch method like that: git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/|\1/' and added color in PS1 like in your linked tutorial. Sep 12, 2013 at 7:24
  • There needs to be a correction to this answer to include the fact that it will display branch if and only if your current directory is a repository. See my post : askubuntu.com/a/766060/295286 May 1, 2016 at 20:36
  • 1
    Also see the bash manual for an explanation of all the magic symbols.
    – djvg
    Oct 23, 2018 at 13:25
-1

Add this one liner to the .bashrc:

export PS1='\u@\h \W$(__git_ps1 " [ - %s - ]") \$ '
-1

You can also add the following line to your .bashrc:

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\`__git_ps1`\$ '
1
  • And what is __git_ps1?
    – muru
    Feb 12, 2016 at 19:42
-2

This will add color to the branch name in terminal

git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[1;31m\]'"\$(git_branch)\[\033[00m\]$ "

Not the answer you're looking for? Browse other questions tagged .