ckdk's blog

在ubuntu终端下显示git分支信息

2016/09/22

ubuntu终端显示git分支信息

在ubuntu下使用git,在进到git目录里发现目录后面不显示分支信息。查资料得出解决方案,需要将以下代码加入到bashrc中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
source ~/.git-completion.bash

function git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
echo "("${ref#refs/heads/}")";
#echo "("${ref#refs/heads/}") ";
}

function parse_git_dirty {
local git_status=$(git status 2> /dev/null | tail -n1) || $(git status 2> /dev/null | head -n 2 | tail -n1);
if [[ "$git_status" != "" ]]; then
local git_now; # 标示
if [[ "$git_status" =~ nothing\ to\ commit || "$git_status" =~ Your\ branch\ is\ up\-to\-date\ with ]]; then
git_now="=";
elif [[ "$git_status" =~ Changes\ not\ staged || "$git_status" =~ no\ changes\ added ]]; then
git_now='~';
elif [[ "$git_status" =~ Changes\ to\ be\ committed ]]; then #Changes to be committed
git_now='*';
elif [[ "$git_status" =~ Untracked\ files ]]; then
git_now="+";
elif [[ "$git_status" =~ Your\ branch\ is\ ahead ]]; then
git_now="#";
fi
echo "${git_now}";
fi
}

#PS1="[\[\e[1;35m\]\u\[\e[1;36m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m]\$"
PS1="[\[\e[1;35m\]\u\[\e[1;32m\]\w\[\e[0m\]] \[\e[0m\]\[\e[1;36m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m\]]\$"

git-completion.bash可以在网上自行google下载。

CATALOG
  1. 1. ubuntu终端显示git分支信息