threads / bug / 63529

Bug in git branch --show-current

Subject: Bug in git branch --show-current

## tl;dr

3 messages between May 27, 2025 and May 27, 2025.

replies: 2people: 2as markdown or json

Ulrich Wittenberg· May 27, 2025, 07:00 UTC · lore

Thank you for filling out a Git bug report! Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue) This is part of a Jenkins job, so for some reason we are working on a detached head

What did you expect to happen? (Expected behavior) I need to find out▒which branch the script is working on, so I used "git branch --show-current" to get this info.

What happened instead? (Actual behavior) The command returns absolutely nothing.

What's different between what you expected and what actually happened? Well, everything!

Anything else you want to add: I tried some other git commands that usually also show information about the current branch, but *none* of them work or even show a fatal error.

[System Info] git version: git version 2.39.5 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh uname: Linux 6.1.0-34-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.135-1 (2025-04-25) x86_64 compiler info: gnuc: 12.2 libc info: glibc: 2.36 $SHELL (typically, interactive shell): /bin/zsh

[Enabled Hooks]

Ulrich Wittenberg Senior Software Developer | KISTERS Oldenburg Germany

ulrich.wittenberg@kisters.de www.kisters.de

________________________________ Ulrich Wittenberg - Energy - KISTERS AG - Haselriege 13 - 26125 Oldenburg - DE | +49 441 93602 -156 | ulrich.wittenberg@kisters.de | www.kisters.de | Handelsregister Aachen, HRB-Nr. 7838 | Vorstand: Klaus Kisters, Hanns Kisters | Aufsichtsratsvorsitzender: Dr. Thomas Klevers This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

Kristoffer Haugsbakk· May 27, 2025, 07:24 UTC · re: Ulrich Wittenberg · lore

Re: Bug in git branch --show-current

On Tue, May 27, 2025, at 09:00, Ulrich Wittenberg wrote:
> What happened instead? (Actual behavior)
> The command returns absolutely nothing.
This is the documented behavior for detached HEAD.

It sounds like you want to know what branches point to the commit you are detached at?

    git for-each-ref --format='%(refname:short)' --points-at=HEAD refs/heads/
-- 
Kristoffer Haugsbakk
Kristoffer Haugsbakk· May 27, 2025, 08:36 UTC · lore

Re: Bug in git branch --show-current

Remember to reply-all on this list. Thanks.
On Tue, May 27, 2025, at 09:58, Ulrich Wittenberg wrote:
> Well, the commit hat I am sitting on was made on a branch. The question
> is: What ist he name oft he branch?

You say that this is a Jenkins job where it’s on detached HEAD. Git doesn’t record in the Git database itself what branch the user was on when the commit was made (if any since you can commit on detached HEAD). Unlike Mercurial (as I’ve heard, I haven’t used it), Git doesn’t store that information. A branch in Git points to a commit.

But the ephemeral reflog (git-reflog(1)) can record how a branch (and HEAD) was updated. From that you can find out what branch the user was on when a commit was made. But the reflog is local only.

But I wonder if you really need this information. I’ve never had a need for it. Git is not designed in such a way that makes querying this information simple.

Show 7 quoted lines
> I tried your command and it also return nothing.
> All other commands also suddenly keep it Top Secret on what branch I am.
> Only an interactive "git log" shows it in the first line, but if you
> pipe that output into another command, this text also is suppressed.
>
> My next idea ist o dump the branch name (which the jenkins job knows)
> into a file and continue from there...
You might want to describe

• What commit Jenkins is on • Where the branch or branches of interests point to • How you expect branches to work

← back to recent threads