I\'m from a Subversion background and, when I had a branch, I knew what I was working on with \"These working files point to this branch\".
But with Git I\'m not sur
As of version 2.22 of git you could just use:
git branch --show-current
As per man page:
Print the name of the current branch. In detached HEAD state, nothing is printed.
I recommend using any of these two commands.
git branch | grep -e "^*" | cut -d' ' -f 2
OR
git status | sed -n 1p | cut -d' ' -f 3
OR (more verbose)
git status -uno -bs| cut -d'#' -f 3 | cut -d . -f 1| sed -e 's/^[ \t]//1'| sed -n 1p
git branch show current branch name only.
While git branch will show you all branches and highlight the current one with an asterisk, it can be too cumbersome when working with lots of branches.
To show only the branch you are currently on, use:
git rev-parse --abbrev-ref HEAD
For my own reference (but it might be useful to others) I made an overview of most (basic command line) techniques mentioned in this thread, each applied to several use cases: HEAD is (pointing at):
Results:
git branch | sed -n '/\* /s///p'
master(detached from origin/master)(detached from origin/feature-foo)(detached from v1.2.3)(HEAD detached at 285f294)(detached from 285f294)git status | head -1
# On branch master# HEAD detached at origin/master# HEAD detached at origin/feature-foo# HEAD detached at v1.2.3# HEAD detached at 285f294# HEAD detached at 285f294git describe --all
heads/masterheads/master (note: not remotes/origin/master)remotes/origin/feature-foov1.2.3remotes/origin/HEADv1.0.6-5-g2393761cat .git/HEAD:
ref: refs/heads/mastercat: .git/HEAD: Not a directorygit rev-parse --abbrev-ref HEAD
masterHEADgit symbolic-ref --short HEAD
masterfatal: ref HEAD is not a symbolic ref(FYI this was done with git version 1.8.3.1)
The following shell command tells you the branch that you are currently in.
git branch | grep ^\*
When you don't want to type that long command every time you want to know the branch and you are using Bash, give the command a short alias, for example alias cb, like so.
alias cb='git branch | grep ^\*'
When you are in branch master and your prompt is $, you will get * master as follows.
$ cb
* master
Add it to PS1 using Mac :
PS1='\W@\u >`[ -d .git ] && git branch | grep ^*|cut -d" " -f2`> $ '
Before running the command above :
After running that command :
Dont worry, if it is not GIT repository , it will not display error because of [-d .git] which checks if .git folder exists or not.