Search code examples
gitgarbage-collectiongit-branchgit-taggit-detached-head

Fixing detached head with a tag


Is it possible to fix a detached head with a tag? In other words, is it possible to prevent garbage collector deleting commits in a detached head with a tag at the last commit?

Every other post that I've found talk about creating a branch in the detached head to prevent losing commits that have been created in this state before leaving to another branch.

As I understand, the major difference between a tag and a branch is the branch moving with the head compared to the tag which stays at the exact same position where it was created.

In my scenario, I've checked out with an SHA-1 and then done a couple of commits from this point. Before leaving, I want to put a bookmark and I think in term of semantic, a tag would be better than a branch to go back later.

I don't have any problem creating a branch at this point instead of a tag if this is the only thing that I can do but I'm wondering about the behavior of git internally with tags and branches with relation with a detached head.

Also, conceptually, if someone does not agree with me about creating a tag instead of a branch, in this case, an explanation would be appreciated.


Solution

  • Yes, a tag will prevent garbage collection the same way a branch does. Only unreachable commits are eligible for garbage collection, and git checks every ref to check if the commit is reachable.