1

I have updated my bash file so that the current Git branch is displayed followed the instructions here. What I ended up using is

PS1="\u@\h \W \$(__git_ps1)\$ " 

However...

  1. I can't see to be able to replace the (branch) to [branch], i.e. show the branch name within brackets instead of parentheses.
  2. The original version had colours:

    PS1="\[$GREEN\]\t\[$RED\]-\[$BLUE\]\u\[$YELLOW\]\[$YELLOW\]\w\[\033[m\]\[$MAGENTA\]\$(__git_ps1)\[$WHITE\]\$ " 

    but when I used this I did not see any colours. How could I see colours and what would be a standard setting for the shell to use?

6
  • About 1), try PS1="\u@\h \W \$(__git_ps1 "[%s]")\$ " instead. Commented Sep 18, 2014 at 12:20
  • 1
    You've tagged your question with both the linux tag and the git-bash tag. Are you using Git on Windows or Linux? Commented Sep 18, 2014 at 12:23
  • @Jubobs:Linux. Is that tag for windows? Commented Sep 18, 2014 at 13:52
  • Git Bash is the bash shell that ships with the Windows version of Git. Commented Sep 18, 2014 at 13:54
  • Regarding 2), could you edit your question to add your colored version of the prompt? If the colors worked in the original definition, there must be something wrong in yours. Commented Sep 18, 2014 at 13:59

3 Answers 3

4

1 - Delimit the branch name with brackets instead of parens

Here is a relevant passage of the .git-prompt.sh file (which, in modern Git versions, contains the definition of the __git_ps1 function):

# 3a) Change your PS1 to call __git_ps1 as # command-substitution: # Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' # ZSH: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' # the optional argument will be used as format string. 

__git_ps1 accepts an optional argument that you can use to customize the format of the string. In your case, you should use

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

2 - Using colors

You can use colors as in the code in your question, but you need to make sure that the variables in question are defined. Put the following lines somewhere in your ~/.bashrc file:

RED=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) BLUE=$(tput setaf 4) MAGENTA=$(tput setaf 5) WHITE=$(tput setaf 7) RESET=$(tput setaf 0) 

After sourcing your ~/.bashrc file, you'll be able to use those colors. For instance, here is a simplified version of your prompt with the current branch name (and surrounding brackets) highlighted in red:

PS1="\W \[$RED\$(__git_ps1 '[%s]')\]\[$RESET\$\] " 

enter image description here

Sign up to request clarification or add additional context in comments.

11 Comments

The colors still don't work.Unless I needed to do something in the part where you mention: After sourcing your .bashrc file.
Yes; sourcing the .bashrc file is a necessary step. Try to close your terminal/console, open it again (.bashrc will be sourced then) and then redefefine the prompt.
We have some progress. Using your PS1 for test everything (the prompt and the commands I input) are in gray now
@Jim Ok. cd to a Git repository, now. The current branch should appear in red.
I did. Everything is gray.
|
1
  1. The __git_ps1 function you're using takes a format string as an argument. So you can pass in whatever you want with %s where you want the branch name to show up. For example:

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

No clue about the colors, sorry.

Comments

0

This works:

function parse_git_dirty { [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*" } function parse_git_branch { git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/" } PS1="\[$Blue\][\[$Cyan\]\d\[$Blue\]] " # Display date PS1=$PS1"\[$Yellow\]\@:" # Display time PS1=$PS1"\[$BGreen\]\w" # Display a green pwd PS1=$PS1"\[$BCyan\]"'$(parse_git_branch)' # Display a cyan git-branch PS1=$PS1"\[$Color_Off\]$ " # Turn off color and end prompt export PS1=$PS1 

it produces something like this:

[Thu Sep 18] 12:44 PM:~/repos/test[master*]$ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.