Linux 配置 Git 命令提示符

Linux 配置 Git 命令提示符

Git 命令补全设置

1
2
3
4
# 这个目录是源码包解压后的目录
cd ./contrib/completion
cp git-completion.bash git-prompt.sh /etc/profile.d/
cd /etc/profile.d/ && ln -s git-completion.bash git-completion.sh

定制git命令提示符

1
2
3
4
5
6
7
8
cat << 'EOF' > /etc/profile.d/git.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWUPSTREAM="verbose git svn"
export GIT_HOME=/usr/local/git
export PATH=$PATH:$GIT_HOME/bin
EOF

主要是 $(__git_ps1 “(%s)”)

写法一

1
2
3
cat << 'EOF' >> /etc/bashrc
export PS1='\[\e[1;32m\][\u@\h \W\[\033[1;31m\]$(__git_ps1 "(%s)")\[\e[1;32m\]]\$ \[\e[0m\]'
EOF

写法二

最好自定义的这些参数都写在if里面,避免非登陆时的bash中变量多次叠加。

1
2
3
4
if shopt -q login_shell ; then
export PS1='\[\e[1;32m\][\u@\h \W]\$ \[\e[0m\]'
export PS1='$(__git_ps1 "(%s)")'$PS1
fi

这个每次执行bash都会叠加命令提示符号

这个便不会,主要是写在了if里面 :if shopt -q login_shell ; then

写法三

个人更喜欢这种写法,有git和无git按个人环境配置即可

1
2
3
4
if shopt -q login_shell ; then
# export PS1='\[\e[1;32m\][\u@\h \W]\$ \[\e[0m\]' # 无Git环境
export PS1='\[\e[1;32m\][\u@\h \W\[\033[1;31m\]$(__git_ps1 "(%s)")\[\e[1;32m\]]\$ \[\e[0m\]' # 有Git环境
fi

附:

CentOS 默认PS1变量

1
PS1=[\u@\h \W]\$
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2018-2024 Outsrkem
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信