Git 分支删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2342821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:56:51  来源:igfitidea点击:

Git branch deletion

gitbranchgit-branch

提问by bobobobo

In Git, what does "deletion" of a branch mean?

在 Git 中,“删除”一个分支是什么意思?

Will it be gone from the repository? Or will it still be navigable to via git branch?

它会从存储库中消失吗?或者它仍然可以导航到 via git branch

What I really want to do is mark a branch as "dead end", i.e., the branch is so far from master, that nobody should use it as a starting point, though there were some good ideas down that branch, so we'd like to keep it, for reference.

我真正想做的是将一个分支标记为“死胡同”,即该分支离 master 太远了,没有人应该使用它作为起点,尽管该分支下有一些好主意,所以我们会喜欢就收藏起来,供参考。

回答by Dan Moulding

You can delete the branch, but tag it first, so that it's history doesn't disappear. This way, the branch doesn't show up in the branch list, which should hopefully deter people from working on it, but the work won't be permanently erased (even after garbage collection is run). For instance, whenever I have a branch that has become irrelevant, but I'm not prepared to permanently delete it, I tag it as "archive/<branch-name>".

您可以删除该分支,但先对其进行标记,这样它的历史记录就不会消失。这样,分支不会出现在分支列表中,这有望阻止人们处理它,但工作不会被永久删除(即使在运行垃圾收集之后)。例如,每当我有一个无关紧要的分支,但我不准备永久删除它时,我会将其标记为“存档/<分支名称>”。

While on masteror some other branch:

master或其他分支上时:

git tag archive/foo foo
git branch -D foo

This creates a tag named archive/foofrom the foobranch before deleting foo. You can also add a message to the tag, that explains what is in the branch, why it existed, why it's now a dead end, etc.

这会在删除之前创建一个archive/foofoo分支命名的标记foo。您还可以向标签添加一条消息,解释分支中的内容、存在的原因、为什么现在是死胡同等等。

git tag -m 'Foo is deprecated in favor of bar' archive/foo foo

The ability to record why a branch is being deprecated is perhaps an advantage of tagging versus moving branches to an alternate namespace.

记录分支被弃用的原因的能力可能是标记与将分支移动到备用命名空间相比的优势。

If you ever need to resurrect a branch that has been archived this way, it's as simple as:

如果您需要恢复以这种方式存档的分支,则非常简单:

git branch foo archive/foo
git tag -d archive/foo       # Optional

Now the branch is back as though it was never deleted.

现在分支又回来了,就好像它从未被删除过一样。

回答by Stewie Griffin

git branch -D <branchName>will delete your branch from repository. You will not be able to see it or navigate anymore. Also you will lose all file changes made in that branch.

git branch -D <branchName>将从存储库中删除您的分支。您将无法再看到它或导航。此外,您将丢失在该分支中所做的所有文件更改。

https://git-scm.com/docs/git-branch

https://git-scm.com/docs/git-branch

回答by Andrew Aylett

Git branches are stored as references to a revision. If you delete the branch, the reference is removed; if nothing else references that revision, it will eventually be garbage collected. Also, if you delete the branch then it's properly gone (from your repository). If you want to mark a branch as deprecated but keep it around for later use, you could move the branch to a subdirectory:

Git 分支存储为对修订的引用。如果删除分支,则删除引用;如果没有其他内容引用该修订版,它最终将被垃圾收集。此外,如果您删除分支,那么它就会正确消失(从您的存储库中)。如果您想将一个分支标记为已弃用但将其保留以备后用,您可以将该分支移动到子目录:

$ git branch
* master
  testing_feature_one
  testing_feature_two
$ git branch -m testing_feature_one deprecated/testing_feature_one
$ git branch
  deprecated/testing_feature_one
* master
  testing_feature_two

Alternatively, you could create a separate repository for deprecated branches, pull them across then delete them from the original. In either case, you're going to affect any users who are following the branches -- the content of their repository won't change (and neither will any of their branch names), but if they try to pull again, they'll have to change their target in their configuration.

或者,您可以为已弃用的分支创建一个单独的存储库,将它们拉过来,然后从原始分支中删除它们。在任何一种情况下,您都会影响关注分支的任何用户——他们的存储库的内容不会改变(他们的任何分支名称也不会改变),但是如果他们再次尝试拉取,他们会必须在他们的配置中改变他们的目标。

回答by Fake Code Monkey Rashid

It won't be navigable via git branch and until garbage collection is performed, it won't be lost from the repository.

它不会通过 git branch 导航,并且在执行垃圾收集之前,它不会从存储库中丢失。

If you want to mark the branch in question as a dead end then simply do so (git may not do this for you but you certainly can)!

如果您想将有问题的分支标记为死胡同,那么只需这样做(git 可能不会为您执行此操作,但您当然可以)!

Labelling it (in any way you prefer) as a historical reference works.

将它(以您喜欢的任何方式)标记为历史参考作品。