Skip to content

Instantly share code, notes, and snippets.

@yisibl
Last active June 21, 2024 01:14
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
@lehuo
Copy link

lehuo commented Nov 29, 2019

非常感谢!提高了生产力

@LeslieYip
Copy link

铁子牛逼嗷,我的Mac上能行

@danchaofanx
Copy link

NBNB

@Sinosaurus
Copy link

搞定,谢谢

@DenghuaCN
Copy link

thk!

@wait-cell
Copy link

mac 10.15后 无法正常工作,求助

@missMHWGO
Copy link

missMHWGO commented Nov 13, 2020

只有我一个人改完后变成这样了吗。。。暴风哭泣。。。

ps.mac 10.15.5
help!!!

@oldmtn
Copy link

oldmtn commented Nov 22, 2020

也可以修改终端的格式和默认的类似,如下:
export PS1='\h:[\033[01;36m]\W[\033[01;32m]$(git_branch)[\033[00m] \u $ '
==>
huanghao-Pro:coturn (master) huanghao $

@UyNewNas
Copy link

UyNewNas commented Dec 9, 2020

666

@UyNewNas
Copy link

UyNewNas commented Dec 9, 2020

只有我一个人改完后变成这样了吗。。。暴风哭泣。。。

ps.mac 10.15.5
help!!!

因为是zsh环境吧 你切到bash下就可以了

@UyNewNas
Copy link

UyNewNas commented Dec 9, 2020

我研究了下,以下是macOS 10.15.7版本下已经成功的例子

    1. bash下:
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\] \$ '
#show git branch
autoload -U colors && colors;
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
}
function m32(){
    echo "\e[01;32m";
}
function m36(){
    echo "\e[01;36m";
}
function m0(){
    echo "\e[0m";
}
function dir() {
    echo "\e[01;36m$1~\e[0m";
}
export PS1="%n@%m $(m36)%1~$(m0) $(m32)\$(git_branch)$(m0) \$ ";

但是我不知道怎么实现$(git_branch)变量的刷新,这很尴尬

@arthurmjt
Copy link

Thx~

@liyahuan
Copy link

mac下,可以把这段代码写在
.bash_profile
文件中,
然后执行
source ~/.bash_profile
这样每次打开终端,都会显示所在分支了

@walterzhaoJR
Copy link

THX~

@qszhang-hub
Copy link

thanks!

@Lumersgo
Copy link

Lumersgo commented Jul 12, 2023 via email

@Huixxi
Copy link

Huixxi commented Aug 11, 2023

赞👍

@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