简体   繁体   English

git删除分支而不删除提交

[英]git remove branch without removing commits

I have two branches I normally work on: master and homepage_buildout. 我有两个我通常工作的分支:master和homepage_buildout。 I was recently told to checkout an old commit from before I was working on the project. 最近我被告知在我开展项目之前检查一个旧的提交。 I simply did: 我只是做了:

 git checkout [commit number]

Then I decided to make this old commit a branch like so: 然后我决定让这个旧的提交像这样的分支:

 git checkout -b old_homepage

Now, I realized that I don't need to be able to reference the branch old_homepage anymore, but I still want it exist as a commit. 现在,我意识到我不再需要能够引用分支old_homepage ,但我仍然希望它作为提交存在。 How do I retain those commits but remove the branch from appearing when I type: 如何保留这些提交,但在键入时删除分支:

 git branch

I think what you're actually asking boils down to: "if I do git branch -d old_homepage will the commits get lost", and the answer is: "Deleting the name only deletes the name ; the commits themselves stick around as long as you can see them in git log --all , and actually even longer." 认为你实际上要问的是:“如果我做git branch -d old_homepage会让提交丢失”,答案是:“删除名称只会删除名称 ;提交本身就会一直存在你可以在git log --all看到它们 - 所有,甚至更长。“

To visualize this better, run something like gitk --all or gitk --tags (try both on some complex git repos). 为了更好地想象这一点,运行类似gitk --allgitk --tags (在一些复杂的git repos上试用)。 Scroll around through the commits. 滚动提交。 Now imagine putting a sticky note on any commit. 现在想象一下在任何提交上粘贴便条。 That's a "tag" or "branch" name. 这是一个“标签”或“分支”名称。 Take the sticky note off. 取下粘滞便笺。 Commit is still there, right? 承诺仍在那里,对吧? :-) :-)

That leaves one obvious question: when do things like commits actually go away? 这留下了一个明显的问题:什么时候提交实际上会消失? The answer is: only after you remove all the names that lead to them. 答案是:只有在您删除了所有通往它们的名称之后。 The gitk command (like most other git commands) starts with the name HEAD . gitk命令(与大多数其他git命令一样)以名称HEAD开头。 If you give it --all it adds all the branch names it can find. 如果你给它--all它会添加它可以找到的所有分支名称。 If you give it --tags it adds all the tag names it can find. 如果你给它--tags它会添加它可以找到的所有标记名称。 Then it works backwards, looking at each commit to see what other commit(s) it refers to. 然后它向后工作,查看每个提交以查看它引用的其他提交。

Git removes things (commits, etc) when there is no way to find them by starting at one of these obvious names and working backwards. 当没有办法通过从这些显而易见的名称开始并向后工作时,Git会删除事物(提交等)。 (Even then it waits a long time, 30 to 90 days by default.) So, if you add a branch or tag label to an old commit whose number you found by doing git log for instance, that gives you a new obvious name for that commit number; (即使这样,它会等待很长时间,默认情况下为30到90天。)因此,如果您将一个分支或标记标签添加到旧的提交中,例如通过执行git log找到它的编号,这会为您提供一个新的明显名称提交号码; but removing again it is safe as long as it's not the only way to find it by name. 但是再次删除它是安全的,只要它不是通过名称找到它的唯一方法。

You can tag the the commit: 您可以标记提交:

$ git tag <tagname> <commit-hash>

Since a tag references the commit, it won't get garbage-collected. 由于标记引用了提交,因此不会进行垃圾回收。

I'm not quite sure but in that situation you would usually use a tag. 我不太确定,但在那种情况下你通常会使用标签。

git tag <some-name> <commit-id>

It would not show up in git-branch but git-tag will show it. 它不会出现在git-branchgit-tag会显示出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM