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 sure when I am editing a file in NetBeans or Notepad++, whether it's tied to the master or another branch.
There's no problem with git
in bash, it tells me what I'm doing.
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)
(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 285f294
git describe --all
heads/master
heads/master
(note: notremotes/origin/master
)remotes/origin/feature-foo
v1.2.3
remotes/origin/HEAD
v1.0.6-5-g2393761
cat .git/HEAD
:ref: refs/heads/master
cat: .git/HEAD: Not a directory
git rev-parse --abbrev-ref HEAD
master
HEAD
git symbolic-ref --short HEAD
master
fatal: ref HEAD is not a symbolic ref
(FYI this was done with git version 1.8.3.1)
Returns either branch name or SHA1 when on detached head:
This is a short version of @dmaestro12's answer and without tag support.
will show only the branch name
The following shell command tells you the branch that you are currently in.
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.When you are in branch master and your prompt is
$
, you will get* master
as follows.you can also use GIT_BRANCH variable as appears here: https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin
The git plugin sets several environment variables you can use in your scripts:
GIT_COMMIT - SHA of the current
GIT_BRANCH - Name of the branch currently being used, e.g. "master" or "origin/foo"
GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
GIT_URL - Repository remote URL
GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
GIT_AUTHOR_EMAIL - Committer/Author Email
GIT_COMMITTER_EMAIL - Committer/Author Email
Add it to
PS1
using Mac :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.