Jayan
Jayan

Reputation: 18459

git tag -l --merged does not show any tags

I am using git tag -l --merged to see the tags that are applicable to my current branch. This worked every where.

Today in one of the repository (created by Jenkins git plugin), the command returns nothing.

What would be causing this?

Upvotes: 1

Views: 878

Answers (1)

torek
torek

Reputation: 488003

Start with git status

The command git tag --merged means git tag --merged HEAD. That is, --merged needs a specific commit ID, which defaults to HEAD. (The -l or --list is implied by the --merged option; you can use it, or not, as you like.)

It then finds (and lists) tags that meet the condition "is an ancestor" of the specified commit. Or, equivalently, we can say that this lists tags where HEAD is a descendant of the tagged commit.

Assuming the tags have not changed in any way, if HEAD used to be listed as the descendant of one or more tags, and HEAD is no longer listed as the descendant of those tags, there is only one possible conclusion: that HEAD itself has changed somehow. It's not normal for tags to change: that's kind of the point of a tag, to be an unchanging name for a specific commit. So it's usually pretty safe to assume that the tags have not changed.

On the other hand, it's quite normal for HEAD to change. HEAD always refers to the current commit, and it's quite normal to move about through history (to inspect or test previous commits) and to create new history (i.e., make new commits). So the question now becomes: what is the current value of HEAD?

This value can be modified in two different ways.

  • You can have a so-called "detached HEAD". Here, HEAD contains a commit hash. The value of HEAD is then simply whatever is in HEAD. Use git checkout with some other commit hash to check out some other commit, i.e., to move this detached HEAD around (but see Fix a Git detached head? as well). This is the normal way to work through history.

    There are other ways to move a detached HEAD around, e.g., using git bisect to pinpoint a commit that introduced a bug. If you're in the middle of a bisect operation, you will also be in detached HEAD state. (In this case, finish or terminate the bisect operation.) Or, if you are in the middle of a git rebase that has paused due to your instructions (e.g., using edit in an interactive rebase) or a merge conflict, resume or terminate the rebase.

  • Or, when HEAD is not in this "detached HEAD" state, it contains a branch name. The value of HEAD is then the value of the corresponding branch name—but the branch name itself can be modified.

To check whether HEAD refers to a branch name or is detached, you can use git status:

$ git status
On branch master
...

or:

$ git status
HEAD detached at 8f60064

or similar. "On branch ..." means "not detached". If you are in the middle of a bisect or rebase, a modern Git's git status will tell you that as well. Finishing that will take you back to whatever mode you were in before, probably "on branch branch".

If you are in detached HEAD mode—other than as part of an operation you need to finish—and wish to exit it, you can just check out any branch name with git checkout:

$ git checkout master
Switched to branch 'master'
...

(but note that any commits you made while in this detached HEAD state are now left without a name—for much more, see Fix a Git detached head?).

If you are on a branch, this means that the tip commit of whatever branch you are on now is not a descendant of any of the tags.

If it should be (but now is not), you must have moved the branch name "backwards", using either git branch or git reset. You will need to fix it, which can be more complex than fixing a detached HEAD. It may be as simply as git reset-ing again to put the branch name back on the appropriate commit, but it may not.

If you are simply on the wrong branch, you just need to git checkout the correct branch again, so that you are back on the branch you intend.

Hence, the first thing to do at this point is to check git status. Is your HEAD detached? If so, solve that. If not, are you on the correct branch? If not, solve that. Otherwise, figure out what you did to change the branch name's value, and solve that.

Upvotes: 2

Related Questions