Why not use git-aware shell prompt, which would tell you name of current branch? git status
also helps.
How git-prompt.sh from contrib/
does it (git version 2.3.0), as defined in __git_ps1
helper function:
First, there is special case if rebase in progress is detected. Git uses unnamed branch (detached HEAD) during the rebase process to make it atomic, and original branch is saved elsewhere.
If the
.git/HEAD
file is a symbolic link (a very rare case, from the ancient history of Git), it usesgit symbolic-ref HEAD 2>/dev/null
Else, it reads
.git/HEAD
file. Next steps depends on its contents:If this file doesn't exist, then there is no current branch. This usually happens if the repository is bare.
If it starts with
'ref: '
prefix, then.git/HEAD
is symref (symbolic reference), and we are on normal branch. Strip this prefix to get full name, and striprefs/heads/
to get short name of the current branch:b="${head#ref: }" # ... b=${b##refs/heads/}
If it doesn't start with
'ref: '
, then it is detached HEAD (anonymous branch), pointing directly to some commit. Usegit describe ...
to write the current commit in human-readable form.
I hope that helps.