3

I'm currently using git-prompt.sh to customize my bash prompt (PS1) to show a status of my git repo in my bash prompt.

This stackoverflow answer was very helpful but not exactly what I'm looking for.

My current .bashrc appears like this:

[aj@computer-name my_current_working_directory (git-branch-name)]$

My .gitconfig uses the following:

[color "status"]
  added = green
  changed = yellow
  untracked = red

The questions is. How do I achieve the following?

I would like my bash prompt to continue to appear the way it does above but change the color of the (git-branch-name) based on the status colors I have set in my .gitconfig

Thanks a million!

2
  • 1
    You'll probably need to write scripts and functions that you can call in your prompt config in order to do that. Commented Apr 1, 2014 at 17:19
  • I ended up customizing the the git-prompt.sh file. Here's what I ended up with. github.com/AJ-Acevedo/dotfiles/commit/… Commented Apr 4, 2014 at 3:26

2 Answers 2

4

I wanted a similar feature today and I achieved it by modifying git-prompt.sh as follows:

__git_ps1_colorize_gitstring ()
{
    if [[ -n ${ZSH_VERSION-} ]]; then
        local c_red='%F{red}'
        local c_green='%F{green}'
        local c_clear='%f'
    else
        local c_red='\[\e[31m\]'
        local c_green='\[\e[32m\]'
        local c_clear='\[\e[0m\]'
    fi

    local branch_color=""
    if [ "$w" = "*" ]; then  # modified
        branch_color="$c_red"
    elif  [ -n "$u" ]; then  # untracked
        branch_color="$c_red"
    elif [ -n "$i" ]; then
        branch_color="$c_green"
    else
        branch_color="$c_clear"
    fi

    c="$branch_color$c"
    z="$c_clear$z"
    w=""
    i=""
    s=""
    u=""
    r="$c_clear$r" 
}

And adding the following to my .bashrc:

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWCOLORHINTS=1

This changes the color of my branch name without adding *, %, or = strings to the prompt.

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

Comments

2

I found this snippet from an article about customizing your prompt. This isn't exactly what you're looking for but just needs some small modifications. There are some colors which are not defined in this snippet but can be found here

export PS1=$IBlack$Time12h$Color_Off'$(git branch &>/dev/null;\
if [ $? -eq 0 ]; then \
  echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
if [ "$?" -eq "0" ]; then \
  # @4 - Clean repository - nothing to commit
  echo "'$Green'"$(__git_ps1 " (%s)"); \
else \
  # @5 - Changes to working tree
  echo "'$IRed'"$(__git_ps1 " {%s}"); \
fi) '$BYellow$PathShort$Color_Off'\$ "; \
else \
  # @2 - Prompt when not in GIT repo
echo " '$Yellow$PathShort$Color_Off'\$ "; \
fi)'

also here is a link with a list of customizations for the prompt

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

2 Comments

I'll except this as the answer as it totally gave be the resources to achieve what I was looking for. I also stumbled across github.com/magicmonty/bash-git-prompt which I'm going to check out. Thanks!
The url with your colors snippet no longer has it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.