git PS1 env variable does not work on mac

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10435117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:52:17  来源:igfitidea点击:

PS1 env variable does not work on mac

gitmacrosps1

提问by James.Xu

I have a script(not written by myself) which shows the git branch/svn branch in my command prompt. Does anyone know why this would not work on mac? It works perfectly in linux.

I have a script(not written by myself) which shows the git branch/svn branch in my command prompt. Does anyone know why this would not work on mac? It works perfectly in linux.

From https://github.com/xumingming/dotfiles/blob/master/.ps1:

From https://github.com/xumingming/dotfiles/blob/master/.ps1:

# Display ps1 with colorful pwd and git status
# Acording to Jimmyxu .bashrc
# Modified by Ranmocy
# --

if type -P tput &>/dev/null && tput setaf 1 &>/dev/null; then
    color_prompt=yes
else
    color_prompt=
fi

__repo () {
    branch=$(type __git_ps1 &>/dev/null && __git_ps1 | sed -e "s/^ (//" -e "s/)$//")
    if [ "$branch" != "" ]; then
        vcs=git
    else
        branch=$(type -P hg &>/dev/null && hg branch 2>/dev/null)
        if [ "$branch" != "" ]; then
            vcs=hg
        elif [ -e .bzr ]; then
            vcs=bzr
        elif [ -e .svn ]; then
            vcs=svn
        else
            vcs=
        fi
    fi
    if [ "$vcs" != "" ]; then
        if [ "$branch" != "" ]; then
            repo=$vcs:$branch
        else
            repo=$vcs
        fi
        echo -n "($repo)"
    fi
    return 0
}

if [ "$color_prompt" = yes ]; then
# PS1='\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[33;40m\]$(__repo)\[\e[00m\]$ '
    PS1='\[\e[01;32m\]\u\[\e[00m\]:\[\e[01;34m\]\W\[\e[33m\]$(__repo)\[\e[00m\]$ '
else
    PS1='\u@\h:\w$(__repo)$ '
fi
unset color_prompt

case "$TERM" in
xterm*|rxvt*)
  PS1="\[\e]0;\W\a\]$PS1"
  ;;
*)
  ;;
esac

回答by Rafa? Rawicki

Mac OS X installations of Git don't have __git_ps1included.

Mac OS X installations of Git don't have __git_ps1included.

Use:

Use:

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/()/'"

as a substitution.

as a substitution.

回答by such

The script you provided fails to detect git repos if the command __git_ps1fails. Add this to ~/.bash_profile:

The script you provided fails to detect git repos if the command __git_ps1fails. Add this to ~/.bash_profile:

source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh

Assuming you stored the script file as ~/.ps1, also add:

Assuming you stored the script file as ~/.ps1, also add:

source ~/.ps1


  • This solution also enables tab completion for git.
  • Mac OS X installations of gitdohave __git_ps1 included, thanks sschuberth and cheapener for mentioning git-completion.bash.
  • This solution also enables tab completion for git.
  • Mac OS X installations of gitdohave __git_ps1 included, thanks sschuberth and cheapener for mentioning git-completion.bash.

回答by Matt Daley

On a new Yosemite mac using built in git, I used this:

On a new Yosemite mac using built in git, I used this:

source /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export PS1='\[\e]0;\u@\h: \w\a\]\[\e[32;1m\]\u@\h:\w \[\e[33;1m\]$(__git_ps1 "[%s] ")\[\e[32;1m\]$ \[\e[0m\]'

Note: on El Capitan, I had to change the path of the git scripts to /Applications/Xcode.app/Contents/Developer/usr/share/git-coreand I guess you have to have XCode installed for this to work.

Note: on El Capitan, I had to change the path of the git scripts to /Applications/Xcode.app/Contents/Developer/usr/share/git-coreand I guess you have to have XCode installed for this to work.

回答by jonas_jonas

If you installed git through macports (git-core), you should add the following to ~/.bash_profile:

If you installed git through macports (git-core), you should add the following to ~/.bash_profile:

source /opt/local/etc/profile.d/bash_completion.sh
source /opt/local/share/git-core/git-prompt.sh  

The location of the git-prompt.sh seemed to have changed a few times.

The location of the git-prompt.sh seemed to have changed a few times.