Why I can't get the branch name

Replace this template with your information

Normally i run below command to get the branch name

$ git rev-parse --abbrev-ref HEAD


$ git branch --show-current

But when run in Gitlab pipeline, I got below output

* (HEAD detached at c88f7d2)

anything sepecial in Gitlab pipeline?

(don’t tell me to use variable CI_COMMIT_REF_NAME, that’s not guarantee to get the current branch name always)

Hi @ozbillwang :wave:

Was your pipeline running on an MR? If yes, that might be why you are seeing that HEAD detached at c88f7d2 since MR pipelines run as detached.

CI_COMMIT_REF_NAME , that’s not guarantee to get the current branch name always

Why do you say so? :thinking:

If the stage/job is triggered by tag create, I got


this is the stage:

  stage: verify
    - npm ci
    - npm run test

I supposed you were talking about that :slightly_smiling_face:

In that case you actually don’t have a branch name, the tag itself is the name of the “branch” so of course you’ll get that :slightly_smiling_face:

So you know it is not same, just joking, right? Otherwise how can you mix branch name to tag name?

if you can run git branch -a and list all tag name, then I shut up. :slight_smile:

Yes I know that tags and branches are not the same :slightly_smiling_face:

The thing is, tags are associated with a commit, not directly to a branch so, in order to get the branch containing that commit you’d have to use something like git branch --contains <tag name>.

Maybe you could use that to get the branch name on pipelines that are run from a tag?

I strugled with the same problem. git branch --contains <commit> on MR run returned only detached HEAD at ... . No other branches (i.e. no branch the MR was comming from)

Solution for that was to use -r parameter, that forces to print the list of remote branches that given commit is associated with (they are visible on detached HEAD):

git branch -r --contains <commit>