背景
Linux环境下使用Git做某项目代码工程的版本控制时,发现在工程下打开终端后无法显示当前所处分支,很不方便,通过编辑.bashrc
配置文件解决这个问题。
解决
- 在终端中打开bash shell的配置文件
.bashrc
:1
sudo gedit ~/.bashrc
- 在文件末尾添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# These shell commands block are for the purpose of displaying the
# current branch name of the current repository.
find_git_branch ()
{
local dir=. head
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
head=$(< "$dir/.git/HEAD")
if [[ $head = ref:\ refs/heads/* ]]; then
git_branch="(*${head#*/*/})"
elif [[ $head != '' ]]; then
git_branch="(*(detached))"
else
git_branch="(*(unknow))"
fi
return
fi
dir="../$dir"
done
git_branch=''
}
PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[0;32m\]\$git_branch\[\033[0m\] \$ " - 使更改立即生效:
1
source ~/.bashrc
参考
- linux命令行终端显示git当前所在分支