2

I have these branches:

refs/heads/master
refs/heads/feature/dummy

Is there native git command to get only the last part of branch name? (master, dummy in the above example)

I have tried git rev-parse --abbrev-ref HEAD but will get master and feature/dummy

Ajithkumar_sekar
  • 631
  • 9
  • 23
hydradon
  • 1,316
  • 1
  • 21
  • 52
  • 2
    There is no branch named `dummy` in the list you posted. The name of the second branch is `feature/dummy`. Git cannot identify it if you use only `dummy`. Try to run `git checkout dummy` and it will fail. – axiac May 17 '18 at 09:00
  • The branch you are currently on _**is**_ `feature/dummy` – evolutionxbox May 17 '18 at 09:00
  • @axiac, @evolutionxbox yes, I know I am on feature/dummy. What I want is a git command to get `dummy`, not `feature/dummy`. This is because I have further processing that will not work with the forward slash – hydradon May 17 '18 at 09:01
  • Why do you need 'dummy'? If you know what the prefix is, you can filter it out using something like grep. – evolutionxbox May 17 '18 at 09:06
  • 1
    @evolutionxbox I have a analysis on `SonarQube` which will automatically strip the `feature/` part of the branchname. I'll then have some code to hit Sonar's API to get analysis result, I'll need to pass in the short branchname as a param. But yeah, I can try `grep` or tokenize, I just want to know if there is a native git command – hydradon May 17 '18 at 09:09
  • I wouldn't think so, because there is no need in git. Why does SonarQube strip that part out? – evolutionxbox May 17 '18 at 09:10
  • 1
    How about `git rev-parse --abbrev-ref HEAD | xargs basename`? – jub0bs May 17 '18 at 09:12
  • @evolutionxbox not sure, their API's param `branch` only accepts the last part – hydradon May 17 '18 at 09:15
  • @hydradon honestly, it sounds like an issue with SonarQube... – evolutionxbox May 17 '18 at 09:16
  • @Jubobs sorry, forgot to mention that I want a `Command Line` syntax, `xargs` doesn't work :( – hydradon May 17 '18 at 09:16
  • Not sure what you mean by "I want a Command Line syntax"... Do you mean that you want to use Git commands alone, and no other Unix utility? Anyway, I tested the command in a local repo and it works as desired, both for `master` and `feature/dummy`. – jub0bs May 17 '18 at 11:05
  • @Jubobs I mean the Command Line in Windows, not Unix, as this will be executed on my team's Windows build agent :) Anyway I think I'll tokenize it. Thanks anyway! – hydradon May 18 '18 at 01:35

2 Answers2

2

I think there isn't one, simply because git allows forward slashes in branch names. Take a look at git-branch docs to see how the branchname is defined: enter image description here Further more, take a look at the git-check-ref-format docs to understand how can you restrict this, if need be (--allow-onelevel).

With all that in mind, I think git sees the branch name as a whole, including the slash, so it wouldn't need to provide any specific command to parse it, but you can always pipe the branch name to another tool that will filter it out. For example this SO answer addresses such case. You would probably have to use something like: value=${str#*/}.

Mladen B.
  • 2,784
  • 2
  • 23
  • 34
1

The following also seems to work:

$ git name-rev --name-only refs/heads/master
master

This appears to be very similar to the rev-parse solution you proposed yourself. It also appears to work on hashes, e.g. where b51e7569 is the commit pointed to by master:

$ git name-rev --name-only b51e7569
master
PerpetualStudent
  • 822
  • 9
  • 17