Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在Mac、Linux 终端显示 Git 当前所在分支 (2024-1-9) #199

Open
yaogengzhu opened this issue Jan 9, 2024 · 0 comments
Open

在Mac、Linux 终端显示 Git 当前所在分支 (2024-1-9) #199

yaogengzhu opened this issue Jan 9, 2024 · 0 comments

Comments

@yaogengzhu
Copy link
Owner

在Mac、Linux 终端显示 Git 当前所在分支

  1. 进入你的home目录
cd ~
  1. 编辑.bashrc文件
vi .bashrc
  1. 将下面的代码加入到文件的最后处
function git_branch {
   branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
   if [ "${branch}" != "" ];then
       if [ "${branch}" = "(no branch)" ];then
           branch="(`git rev-parse --short HEAD`...)"
       fi
       echo " ($branch)"
   fi
}

export PS1='\u@\h \[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\] \$ '
  1. 保存退出
  2. 执行加载命令
source ./.bashrc
  1. 完成

Mac 下面启动的 shell 是 login shell,所以加载的配置文件是.bash_profile,不会加载.bashrc。如果你是 Mac 用户的话,需要再执行下面的命令,这样每次开机后才会自动生效:

echo "[ -r ~/.bashrc ] && source ~/.bashrc" >> .bash_profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant