20

This question already has an answer here:

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!

marked as duplicate by muru command-line Nov 8 '17 at 6:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 '14 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 '14 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 '17 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 '18 at 12:39
33

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?

  • Is it possible to avoid the brackets and change the color? – Cedric Reichenbach Jun 18 '13 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 '13 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. – Cedric Reichenbach Sep 12 '13 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 – Sergiy Kolodyazhnyy May 1 '16 at 20:36
  • 1
    Also see the bash manual for an explanation of all the magic symbols. – djvg Oct 23 '18 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`\$ '
  • And what is __git_ps1? – muru Feb 12 '16 at 19:42
-1

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 or ask your own question.