Skip to content

Commit d3d717a

Browse files
spearceJunio C Hamano
authored andcommitted
Add current branch in PS1 support to git-completion.bash.
Many users want to display the current branch name of the current git repository as part of their PS1 prompt, much as their PS1 prompt might also display the current working directory name. We don't force our own PS1 onto the user. Instead we let them craft their own PS1 string and offer them the function __git_ps1 which they can invoke to obtain either "" (when not in a git repository) or "(%s)" where %s is the name of the current branch, as read from HEAD, with the leading refs/heads/ removed. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d33909b commit d3d717a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,31 @@
1818
# 2) Added the following line to your .bashrc:
1919
# source ~/.git-completion.sh
2020
#
21+
# 3) Consider changing your PS1 to also show the current branch:
22+
# PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
23+
#
24+
# The argument to __git_ps1 will be displayed only if you
25+
# are currently in a git repository. The %s token will be
26+
# the name of the current branch.
27+
#
2128

2229
__gitdir ()
2330
{
2431
echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
2532
}
2633

34+
__git_ps1 ()
35+
{
36+
local b="$(git symbolic-ref HEAD 2>/dev/null)"
37+
if [ -n "$b" ]; then
38+
if [ -n "$1" ]; then
39+
printf "$1" "${b##refs/heads/}"
40+
else
41+
printf " (%s)" "${b##refs/heads/}"
42+
fi
43+
fi
44+
}
45+
2746
__git_refs ()
2847
{
2948
local cmd i is_hash=y dir="${1:-$(__gitdir)}"

0 commit comments

Comments
 (0)