Fixing detached head with a tag

时间:2017-08-13 13:54:51

标签: git garbage-collection git-branch git-tag git-detached-head

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.

2 个答案:

答案 0 :(得分:2)

将标记保存为将来的某些提交并不是错误的。但是,有一些缺点:

  • git fetchpush带有一些选项会将所有标记复制到目标端,而不进行任何映射。因此,任何私有临时标记都可能以公共名称空间结束。
  • 标签太多可能会导致GUI出现问题

总的来说,我会说标签更多的是作为公共永久里程碑,例如发布。

还有更多选择需要考虑:

  • 有一段时间没有收集提交,也无法通过reflog访问
  • 它可以是一个分支,如果它没有改变则不是问题
  • 它可以是另一个名称空间中的引用,而不是tagsbranches。这是例如bisect对中间标记的作用
  • 它可以合并到专用的collect-all分支。这是我在本地开发中使用的内容

答案 1 :(得分:1)

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.