Write the Code. Change the World.

5月 06

在 win 中,在终端中使用 git 都会显示分支名,这样很清晰,也很安全。知道当前处于哪个分支。而 linux 下,默认是不显示这些的。这个就得想办法去实现。办法有好几种,这里就来一个简单好用的。

操作来一波

# 如果权限不够 sudo 提升权限

sudo vim ~/.bashrc

# 在末尾追求以下内容
# Show git branch name
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

# source 使生效
source ~/.bashrc

到这里,已经看到效果了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注