In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?
One use of this technique would be automatically labeling a release (like svnversion
would do with Subversion).
Please also see my related question: How to programmatically determine whether a Git checkout is a tag, and if so what is the tag name?
Here is what I do:
The output would look like this:
I found that calling git is rather slow (any of the subcommands), especially for updating the prompt. Time varies between .1 and .2 seconds within the root dir of a repo, and over .2 seconds outside a repo, on a top notch machine (raid 1, 8 gb ram, 8 hardware threads). It does run Cygwin, though.
Therefore I wrote this script for speed:
May need some tweaking.
This one worked for me in the bash file.
If you are on a detached head (i.e. you've checked out a release) and have an output from git status such as
And you want the release version, we use the following command...
This returns 1.7.3.1, which we replace in our parameters.yml (Symfony) with
Hope this helps :) Obviously if you have non-numerics in your branch name, you'll need to alter the arguments to the tr command.
you can use
git name-rev --name-only HEAD
adapting the accepted answer to windows powershell: