Shell中显示Git分支名
文章目录
一、Windows下的msys2 bash
如果我们安装了git for windows,在使用git bash的时候,如果当前目录是Git管理的目录,则会在后面显示Git的当前分支名:
如果我们安装了msys2,则可以不用再安装git for windows,因为Git for windows也是使用的msys2中的mingw64,而且占用的空间还不小。
msys2中安装git直接使用下面的命令即可。
1pacman -S git
但是这样安装后不能像Git for windows一样在bash中显示git管理目录的当前分支。 为了达到可以显示的目的,可以看看Git for windows是如何处理的。 以笔者安装的Git for window 64位版本,安装在C:\Program Files\Git为例进行说明。 在C:\Program Files\Git\etc\bash.bashrc中有这样一段脚本:
1# If MSYS2_PS1 is set, use that as default PS1;
2# if a PS1 is already set and exported, use that;
3# otherwise set a default prompt
4# of user@host, MSYSTEM variable, and current_directory
5[[ -n "${MSYS2_PS1}" ]] && export PS1="${MSYS2_PS1}"
6# if we have the "High Mandatory Level" group, it means we're elevated
7#if [[ -n "$(command -v getent)" ]] && id -G | grep -q "$(getent -w group 'S-1-16-12288' | cut -d: -f2)"
8# then _ps1_symbol='\[\e[1m\]#\[\e[0m\]'
9# else _ps1_symbol='\$'
10#fi
11[[ $(declare -p PS1 2>/dev/null | cut -c 1-11) = 'declare -x ' ]] || \
12 export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n'"${_ps1_symbol}"' '
13unset _ps1_symbol
14
15# Uncomment to use the terminal colours set in DIR_COLORS
16# eval "$(dircolors -b /etc/DIR_COLORS)"
17
18# Fixup git-bash in non login env
19shopt -q login_shell || . /etc/profile.d/git-prompt.sh
可以看到最后是调用了/etc/profile.d/git-prompt.sh,打开C:\Program Files\Git\etc\profile.d\git-prompt.sh,中间有一段脚本:
1if test -f ~/.config/git/git-prompt.sh
2then
3 . ~/.config/git/git-prompt.sh
4else
5 PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
6 PS1="$PS1"'\n' # new line
7 PS1="$PS1"'\[\033[32m\]' # change to green
8 PS1="$PS1"'\u@\h ' # user@host<space>
9 PS1="$PS1"'\[\033[35m\]' # change to purple
10 PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
11 PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
12 PS1="$PS1"'\w' # current working directory
13 if test -z "$WINELOADERNOEXEC"
14 then
15 GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
16 COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
17 COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
18 COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
19 if test -f "$COMPLETION_PATH/git-prompt.sh"
20 then
21 . "$COMPLETION_PATH/git-completion.bash"
22 . "$COMPLETION_PATH/git-prompt.sh"
23 PS1="$PS1"'\[\033[36m\]' # change color to cyan
24 PS1="$PS1"'`__git_ps1`' # bash function
25 fi
26 fi
27 PS1="$PS1"'\[\033[0m\]' # change color
28 PS1="$PS1"'\n' # new line
29 PS1="$PS1"'$ ' # prompt: always $
30fi
这段脚本从change to green那行开始就是Git for windows显示的提示。
我们把这段复制到msys2中的bashrc中去并稍作修改,在msys2的bash中输入:
1vim ~/.bashrc
或者用Windows的其它工具比如Notepad打开也可以。 在文件的最后添加:
1#显示Git的当前分支
2PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
3PS1="$PS1"'\[\033[32m\]' # change to green
4PS1="$PS1"'\u@\h ' # user@host<space>
5PS1="$PS1"'\[\033[35m\]' # change to purple
6PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
7PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
8PS1="$PS1"'\w' # current working directory
9
10GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
11COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
12COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
13COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
14if test -f "$COMPLETION_PATH/git-prompt.sh"
15then
16 . "$COMPLETION_PATH/git-completion.bash"
17 . "$COMPLETION_PATH/git-prompt.sh"
18 PS1="$PS1"'\[\033[36m\]' # change color to cyan
19 PS1="$PS1"'`__git_ps1`' # bash function
20fi
21
22PS1="$PS1"'\[\033[0m\]' # change color
23PS1="$PS1"'\n' # new line
24PS1="$PS1"'# ' # prompt: always #
然后重新打开bash即可。
现在可以把Git for Windows卸载掉了。
二、Mac Bash
Mac下在~/.bash_profile最后添加如下内容:
1#显示Git的当前分支
2PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
3PS1="$PS1"'\[\033[32m\]' # change to green
4PS1="$PS1"'\u@\h ' # user@host<space>
5PS1="$PS1"'\[\033[35m\]' # change to purple
6PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
7PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
8PS1="$PS1"'\w' # current working directory
9
10GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
11COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
12COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
13COMPLETION_PATH="$COMPLETION_PATH/share/git-core" # 这句话与Windows下的不一样,最重要的是要找对路径
14if test -f "$COMPLETION_PATH/git-prompt.sh"
15then
16 . "$COMPLETION_PATH/git-completion.bash"
17 . "$COMPLETION_PATH/git-prompt.sh"
18 PS1="$PS1"'\[\033[36m\]' # change color to cyan
19 PS1="$PS1"'`__git_ps1`' # bash function
20fi
21
22PS1="$PS1"'\[\033[0m\]' # change color
23PS1="$PS1"'\n' # new line
24PS1="$PS1"'# ' # prompt: always #
其中COMPLETION_PATH的路径一定要找正确,笔者的系统的绝对路径为:
设置后,重开Shell,进入一个Git源目录:
可以看到Git的分支名了。
你的关注、点赞、打赏是我写作的动力
- 原文作者:Witton
- 原文链接:https://wittonbell.github.io/posts/2020/2020-08-22-Shell中显示Git分支名/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。