How to extract current branch name from git log
How to extract current branch name from git log
when I use the **
log -n 1 --pretty=%d HEAD
** then the following output is showing (HEAD -> branch1, origin/branch1 , orign/branch3 , origin/branch4 etc....)
But I want to extract only the current branch name from the git log, Can someone please help ?
Limitation: I want to achive this only by using **git log ** command.
CodePudding user response:
So, from comments,
when I deploy the bundle in a dev environment, the head is detached and when I run the commands like (git rev-parse --abbrev-ref HEAD , git branch --show-current ) returning empty
what you're really trying to do is find some ref that points at the current checkout? If HEAD
isn't attached to it, its not "the current branch", it's just a maybe-related name that points at the same commit.
And there's a bundle involved? What's really going on here?
To find the usual shorthand for refs that point at the current checkout,
git for-each-ref --format='%(refname:short)' --points-at HEAD
or if you don't mind scraping comma-separated lists
git log --no-walk --decorate-refs=refs/ --pretty=%D
and to restrict it to listing just local branches, add refs/heads
to the f-e-r or change the log option to --decorate-refs=refs/heads
CodePudding user response:
Pipe your output to sed
to grab only the part you want :
git log -1 --pretty=%d | sed -E 's/^.*HEAD -> ([^\,]*)\,.*$/\1/'
(HEAD
is implied by default, you don't need to explicitly ask for it)
CodePudding user response:
Easiest way I know of is:
git rev-parse --abbrev-ref HEAD
That will write the current branch name only.
CodePudding user response:
We can use the --show-current option of the git-branch command to print the current branch’s name. Here's how:
$ git branch –show-current