何时删除 Git 中的分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5330145/
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 10:22:34  来源:igfitidea点击:

When to delete branches in Git?

gitgithubversion-controlgit-branchgit-gui

提问by Anthony Compton

Suppose we have an application that's stable.

假设我们有一个稳定的应用程序。

Tomorrow, someone reports a big ol' bug that we decide to hotfix right away. So we create a branch for that hotfix off of "master", we name it "2011_Hotfix", and we push it up so that all of the developers can collaborate on fixing it.

明天,有人报告了一个我们决定立即修复的重大错误。因此,我们从“master”为该修补程序创建了一个分支,将其命名为“2011_Hotfix”,然后将其推送,以便所有开发人员都可以协作修复它。

We fix the bug, and merge "2011_Hotfix" into "master" as well as into the current development branch. And push "master."

我们修复了错误,并将“2011_Hotfix”合并到“master”以及当前的开发分支中。并推“主人”。

What do we do with "2011_Hotfix" now? Should it just sit out there as a branch forever until the end of time or should we now delete it, since it has served its purpose? It seems unclean to just leave branches lying around everywhere, as the list of branches will likely become very long, most of which aren't even necessary anymore.

我们现在如何处理“2011_Hotfix”?它应该作为一个分支永远留在那里直到时间结束还是我们现在应该删除它,因为它已经达到了它的目的?到处乱放树枝似乎是不干净的,因为树枝的列表可能会变得很长,其中大部分甚至不再需要了。

In the event that it should be deleted, what will happen to its history? Will that be maintained, even though the actual branch is no longer available? Also, how would I remove a remote branch?

如果它应该被删除,它的历史会发生什么?即使实际的分支不再可用,它也会被维护吗?另外,我将如何删除远程分支?

采纳答案by Artefact2

You can safely remove a branch with git branch -d yourbranch. If it contains unmerged changes (ie, you would lose commits by deleting the branch), git will tell you and won't delete it.

你可以安全地删除一个分支git branch -d yourbranch。如果它包含未合并的更改(即,删除分支会丢失提交),git 会告诉你并且不会删除它。

So, deleting a merged branch is cheap and won't make you lose any history.

因此,删除合并的分支很便宜,并且不会丢失任何历史记录。

To delete a remote branch, use git push origin :mybranch, assuming your remote name is origin and the remote branch you want do delete is named mybranch.

要删除远程分支,请使用git push origin :mybranch,假设您的远程名称是 origin,并且您要删除的远程分支名为 mybranch。

回答by Adam Dymitruk

What you need to do is tag anything that you release. Keep branches around for when you are actively developing.

您需要做的是标记您发布的任何内容。在您积极开发时保留分支。

Delete old branches with

删除旧分支

git branch -d branch_name

Delete them from the server with

从服务器中删除它们

git push origin --delete branch_name

or the old syntax

或旧语法

git push origin :branch_name

which reads as "push nothing into branch_name at origin".

读作“在原点不将任何内容推入 branch_name”。

That said, as long as the DAG (directed acyclic graph) can point to it, the commits will be there in history.

也就是说,只要 DAG(有向无环图)可以指向它,提交就会在历史中。

Google "git-flow" and that may give some more insight on release management, branching and tagging.

谷歌“git-flow”,这可能会提供更多关于发布管理、分支和标记的见解。

回答by chesterbr

Since the question has the "github" tag, I'd also add this: specifically in Github, if you pull-requesta branch and it gets merged (either via the UI or by merging the pull request's branch), you won't lose the pull request data (including comments), even if you remove the branch.

由于问题具有“github”标签,我还要添加以下内容:特别是在Github 中,如果您拉取一个分支并合并它(通过 UI 或通过合并拉取请求的分支),您将不会即使您删除了分支也会丢失拉取请求数据(包括评论)。

A consequence of this: If you incorporate pull requests as a part of your workflow (which blends sweetly with code reviews), you can safely delete branches as soon as they get merged. This is so commonplace that recently Github added a (sweet) feature that pops a "delete branch" button right after you merge a pull request.

这样做的结果是:如果您将拉取请求作为工作流程的一部分(与代码完美融合),您可以在分支合并后立即安全地删除它们。这太常见了,以至于最近 Github 添加了一个(甜蜜的)功能,在您合并拉取请求后立即弹出“删除分支”按钮。

But it is worth noticing that each group should adopt the workflow that suits it best (and it may or may not lead to deleting such branches). My current work team, for example, prunes all branches that are not master or deployment-related (e.g., production, staging, etc.) as soon as their pull requests gets merged, and we still have full tracking of how the related commits formed each incremental improvement of each product.

但值得注意的是,每个组都应该采用最适合自己的工作流(可能会也可能不会导致删除此类分支)。例如,我当前的工作团队会在所有与 master 或部署无关的分支(例如,生产、暂存等)合并后立即删除它们,我们仍然可以全面跟踪相关提交的形成方式每个产品的每一个增量改进。

Of course no history management (pull requests or otherwise) replaces proper tagging of versions (which you preferably automate with the same tool/script that deploys/packages a version), so you can always fast-switch to whatever your users happen to be on at a given moment. Tagging is also the key to solve your original problem: if you establish that any branch merged to the "work" branches can and should be deleted, and that any one that is merged to a version tag, "production", etc. should not, you'll always have the hotfixes alive until they are integrated in a future version.

当然,没有历史管理(拉取请求或其他方式)取代了正确的版本标记(您最好使用部署/打包版本的相同工具/脚本自动化),因此您始终可以快速切换到您的用户碰巧在的任何内容在给定的时刻。标记也是解决原始问题的关键:如果您确定合并到“工作”分支的任何分支都可以并且应该被删除,并且任何合并到版本标记、“生产”等的分支都不应该被删除,您将始终拥有这些修补程序,直到它们被集成到未来版本中。

回答by Mark F Guerra

I would add that the disadvantage of deleting branches is that you will break any hyperlinks to those branches on GitHub (this question is tagged github). You'll get a 404 Not Founderror for those links. This is why I change my links to point to a commit or tag after I delete a branch on GitHub.

我想补充一点,删除分支的缺点是你会破坏 GitHub 上这些分支的任何超链接(这个问题被标记为 github)。您将收到404 Not Found这些链接的错误。这就是我在删除 GitHub 上的分支后更改链接以指向提交或标记的原因。

Because some links can't be changed, such as in email, I now avoid hyperlinking to GitHub branches entirely and link to a commit or tag from day one.

由于某些链接无法更改,例如在电子邮件中,我现在完全避免超链接到 GitHub 分支并从第一天开始链接到提交或标签。

I prefer to delete branches after they're merged in. This prevents the visual clutter of a long list of branches in your repository. These branches also get propagated to all of the repository's forks.

我更喜欢在合并后删除分支。这可以防止存储库中一长串分支的视觉混乱。这些分支也会传播到存储库的所有分支。

First I delete my local branch. This prevents it from being accidentally pushed later.

首先我删除我的本地分支。这可以防止它稍后被意外推动。

git branch -d branchName

Then I delete the remote tracking branch

然后我删除远程跟踪分支

git branch -dr remoteName\branchName

Then I delete the branch on GitHub. I use the web interface, but the equivalent command is below.

然后我删除了 GitHub 上的分支。我使用网络界面,但等效命令如下。

git push remoteName :branchName

Even if the branch is never merged, typically I would still like to keep the commits around for posterity. However I still like to delete the branch. To spread the commits around and to keep them from being eaten by the garbage collector, I make an annotated tag pointing to the same commit as the deleted branch.

即使分支从未合并,通常我仍然希望为后代保留提交。但是我仍然喜欢删除分支。为了分散提交并防止它们被垃圾收集器吃掉,我制作了一个带注释的标记,指向与已删除分支相同的提交。

git tag -a tagName commitOrBranchName

Then I push the tag to github

然后我把标签推送到github

git push remoteName tagName

回答by jfmercer

It seems that you want to delete the 2011_Hotfixbranch without losing its history. I will discuss deletion first and history second.

似乎您想在2011_Hotfix不丢失其历史记录的情况下删除该分支。我将首先讨论删除,然后讨论历史。

The usual gitbranch deletion methods have already been described above, and they work as expected. gitdoes not have a one or two word command that means, "Hey git, delete both the local and remote branch." But this behavior can be mimicked via shell script. For example, take Zach Holman's shell script 'git-nuke'. It is very simple:

git上面已经描述了常用的分支删除方法,它们按预期工作。git没有一两个字的命令,意思是“嘿git,删除本地和远程分支。” 但是这种行为可以通过 shell 脚本来模仿。例如,以Zach Holman 的 shell 脚本 'git-nuke'为例。这很简单:

#!/bin/sh
git branch -D 
git push origin :

Put this in an executable file (e.g., git-nuke) in one of your $PATHdirectories. If you're not on the 2011_Hotfixbranch, you simply running git-nuke 2011_Hotfixwill delete both the local and remote branches. This is much faster & simpler--though perhaps more dangerous--than the standard gitcommands.

将其放在git-nuke您的$PATH目录之一中的可执行文件(例如)中。如果您不在2011_Hotfix分支上,则只需运行git-nuke 2011_Hotfix即可删除本地和远程分支。这比标准git命令更快、更简单——尽管可能更危险。

Your concern about preserving history is a good one. In this case, you needn't be worried. Once you merge 2011_Hotfixonto master, all commits from 2011_Hotfixwill be added to master's commit history. In short, you will not lose history from a simple merge.

您对保存历史的关注是好的。在这种情况下,您不必担心。合并2011_Hotfix到 后master,来自 的所有提交2011_Hotfix都将添加到master的提交历史记录中。简而言之,您不会因为简单的合并而丢失历史记录。

I have one more word to add that is perhaps beyond the scope of your question, but is relevant nonetheless. Let's imagine that there are 20 tiny, "work-in-progress" commits on 2011_Hotfix; however, you want only one complete commit for 2011_Hotfixto be added to master's history. How do you combine all 20 small commits into one big commit? Fortunately, gitallows you to consolidate multiple commits into one commit by using git-rebase. I won't explain here how that works; though, if you're interested, the documentation for git-rebaseis excellent. Do note that git rebaserewrites history, so it should be used judiciously, especially if you are new to it. Finally, your 2011_Hotfixscenario is about a dev team, not a solo dev. If project team members use git rebase, it is wise for the team to have explicit guidelines on the use of git rebasein order that some cowboy dev on the team doesn't unwittingly damage a project's git's history.

我还有一个词要补充,这可能超出了您的问题范围,但仍然相关。假设有 20 个微小的“正在进行的”提交2011_Hotfix;但是,您只想将一个完整的提交2011_Hotfix添加到master的历史记录中。你如何将所有 20 个小提交合并为一个大提交?幸运的是,git允许您使用git-rebase. 我不会在这里解释它是如何工作的;不过,如果您有兴趣,的文档git-rebase非常好。请注意,它会git rebase重写历史,因此应该明智地使用它,尤其是当您不熟悉它时。最后,你的2011_Hotfix场景是关于一个开发团队,而不是一个单独的开发。如果项目团队成员使用git rebase,对于团队来说,明智的做法是制定明确的使用指南git rebase,以免团队中的一些牛仔开发人员无意中破坏了项目的git历史。

回答by Michael Fürstenberg

If it's been successfully merged back and maybe even tagged then I would say it has no use anymore. So you can safely do git branch -d branchname.

如果它已成功合并回来,甚至可能被标记,那么我会说它不再有用了。所以你可以放心地做git branch -d branchname

回答by huch

If you want to prune local branches that have been removed from origin, you can also prune, while using git fetch

如果要修剪已经从原点移除的本地分支,也可以修剪,同时使用 git fetch

git fetch --prune

回答by tkruse

You can delete branches in all major web UIs such as github, BitBucket. After deleting the branch online, you can delete the local branch using

您可以在所有主要的 Web UI 中删除分支,例如 github、BitBucket。在线删除分支后,可以使用删除本地分支

git remote prune origin