61

Say I do the following:

  1. Create branch X
  2. Create Tag t (to branch X)
  3. Push
  4. Remove branch X

What happen to tag t? is it just floating there? is it considered as garbage?

Should I remove all tags pointing at branch before removing the branch itself?

Reference

From Git Basics - Tagging:

Git uses two main types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit.

3 Answers 3

75

What happen to tag t?

Let's say you created branch x from a commit E and then tagged that commit with tag t. E.g.

                           x (branch)
                           |
                           V             
A-----B------C------D------E
                           ^
                           |
                           t (tag)

If you remove branch x nothing happens to tag t.

git branch -D x

The tag still points to commit E.

A-----B------C------D------E
                           ^
                           |
                           t (tag)

is it considered as garbage?

No, because the commit is still referenced by tag t.

What if the commit is removed?

You do not remove commits. You remove pointers to commits and if commits are no longer referenced git will garbage collect them some day (depending on your configuration).

See git gc

Even if you removed all ordinary refs, like branches and tags, the commits will still be referenced in the reflog for some time and you can access them, e.g. re-create a branch, tag them or cherry-pick and so on.

You can see the reflog using git reflog. Also take a look at gc.reflogExpireUnreachable and gc.reflogExpire


EDIT

If somehow git's object database is corrupted. Either a file from .git/objects was deleted (e.g. you accidentially deleted it using your file explorer or a command-line command) or a ref points to a non-existent git object (like a commit, tree or blob object), you will get errors if git tries to access these objects.

Here is a list of errors that might occur when git tries to access an object that does not exist or if a non-existent object is referenced.

  • commit

    fatal: Could not parse object '<ref-name>'.
    

    example:

    fatal: Could not parse object 'master'.
    
  • tree

    fatal: unable to read tree <tree-sha1>
    

    example:

    fatal: unable to read tree 13a3e0908e4f6fc7526056377673a5987e753fc8
    
  • blob

    error: unable to read sha1 file of <blob-name> (<blob-sha1>)
    

    example:

    error: unable to read sha1 file of test.txt (e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)
    

Take a look at Git Internals for a deeper understanding.

2
  • does this mean that if I tag a commit in a branch which is later removed, I will still be able to reference it with the tag. E.g. if I add a tag v1 to the branch x, will the tag v1 still be usable after x is deleted? (Thinking about bug fixes of old releases, when the releases are just tags on commits on master. No specific release branches for each release) Oct 26, 2020 at 8:52
  • 4
    Yes, as long as you do not delete the tag the commit is still referenced and thus will not be deleted. And if you need a branch again you can create a branch that starts at the tag's commit, e.g. git branch <branchname> v1.
    – René Link
    Oct 26, 2020 at 11:45
7

I'm not addressing the specific scenario in the OP's question, but rather the question in the title: What happens to a git tag pointing to a removed commit?

If somehow you did manage to remove a commit that was referenced by a tag (not sure how you could do that - see René Link's answer), the tag would just be a pointer to an invalid commit (you can test this by manually editing a tag from .git/refs/tags).

In such case, output of git tag would be something like this:

$ git tag
error: refs/tags/v1.0 does not point to a valid object!
v1.1
...etc

Checkout would also produce error:

$ git checkout v1.0
fatal: reference is not a tree: v1.0

So the answer to a question "What happens to a git tag that references a removed commit?" is... nothing. It will remain there, pointing to an invalid reference, until you remove it with git tag -d <tag>.

4
  • 1
    So if you want to checkout a tag, which is pointing to a commit in a deleted branch, the tag is invalid?
    – testing
    Sep 15, 2017 at 8:40
  • 1
    @testing no. This answer is addressing a hypothetical situation that OP describes. For a practical scenario, see René's answer above.
    – 1615903
    Sep 15, 2017 at 9:06
  • 4
    Renè's example shows a tag on a commit, which exists after the deletion of the branch. What if the tag is on one of the following commits on branch X (not shown in the example)? Is the tag then pointing on a commit created from the merge? E.g. you had the tag on one of the commits in a feature branch, merge that feature branch into develop, and finally delete the feature branch.
    – testing
    Sep 15, 2017 at 9:11
  • 6
    Deletion of a branch does not delete any commits. Only commits that are unreachable by any tag, branch, or other ref are deleted, and only after the garbage collection runs.
    – 1615903
    Sep 15, 2017 at 11:11
2

If you delete a branch that a tag was created from, this will have no effect on the tag. A tag does not hold a reference of where it was created from.

If you want to know more about tags vs. branches I would recommend looking at this question: How is a tag different from a branch? Which should I use, here?

3
  • See my edit: tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit.. What if the commit is removed?
    – idanshmu
    Oct 22, 2015 at 14:34
  • 2
    How are you 'deleting' a commit? Let's say you are on master, and you tagged your master branch with '1.0' you then want to roll back the latest two commits on master. You would run a 'git reset --hard HEAD~2' which would bring your HEAD back two commits, making those commits 'disappear' from your master branch. However they are still stored in your repo, just not in the master branch. Your tag '1.0' will have no issue still pointing to the correct commit. Oct 22, 2015 at 15:04
  • 1
    Or I guess in your case. If you have commits only in a branch. You delete that branch. Those commits are not being removed. If you use 'git reflog' after you delete the branch, you should still be able to find the SHA1 for your commits from that branch, which is what the tag is pointing to. Oct 22, 2015 at 15:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.