Skip to content

Instantly share code, notes, and snippets.

@yisibl
Last active September 13, 2024 08:59
Show Gist options
  • Save yisibl/8281454 to your computer and use it in GitHub Desktop.
Save yisibl/8281454 to your computer and use it in GitHub Desktop.
在Mac、Linux 终端显示 Git 当前所在分支

在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
@joker0ops
Copy link

If you want the Ubuntu style of bash, please use this sentence at the end of file:
如果你使用的是Ubuntu的bash,可以使用下面这行代码让终端格式与默认格式相似
export PS1='\[\033[32m\u@\h:\033[0m\[\033[01;34m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\]\$'
参考链接:https://zhuanlan.zhihu.com/p/133291342

@Lumersgo
Copy link

Lumersgo commented Oct 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment