删除 git 中的分支是否会将其从历史记录中删除?

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

Does deleting a branch in git remove it from the history?

gitversion-controlbranchrevision-history

提问by Ken Liu

Coming from svn, just starting to become familiar with git.

来自 svn,刚开始熟悉 git。

When a branch is deleted in git, is it removed from the history?

在 git 中删除分支时,它是否从历史记录中删除?

In svn, you can easily recover a branch by reverting the delete operation (reverse merge). Like all deletes in svn, the branch is never really deleted, it's just removed from the current tree.

在 svn 中,您可以通过还原删除操作(反向合并)轻松恢复分支。像 svn 中的所有删除一样,分支从未真正删除,它只是从当前树中删除。

If the branch is actually deleted from the history in git, what happens to the changes that were merged from that branch? Are they retained?

如果分支实际上是从 git 中的历史记录中删除的,那么从该分支合并的更改会发生什么情况?他们被保留了吗?

回答by CB Bailey

Branches are just pointers to commits in git. In git each commit has a complete source tree, it is a very different structure from svn where all branches and tags (by convention) live in separate 'folders' of the repository alongside the special 'trunk'.

分支只是指向 git 中提交的指针。在 git 中,每个提交都有一个完整的源代码树,它与 svn 的结构非常不同,svn 的所有分支和标签(按照惯例)都位于存储库的单独“文件夹”中,并与特殊的“主干”一起存在。

If the branch was merged into another branch before it was deleted then all of the commits will still be reachable from the other branch when the first branch is deleted. They remain exactly as they were.

如果分支在被删除之前被合并到另一个分支,那么当第一个分支被删除时,所有提交仍然可以从另一个分支访问。他们保持原样。

If the branch is deleted without being merged into another branch then the commits in that branch (up until the point where the forked from a commit that is still reachable) will cease to be visible.

如果该分支在没有合并到另一个分支的情况下被删除,那么该分支中的提交(直到仍然可以访问的提交的分叉点)将不再可见。

The commits will still be retained in the repository and it is possible to recover them immediately after the delete, but eventually they will be garbage collected.

提交仍将保留在存储库中,删除后可以立即恢复它们,但最终它们将被垃圾收集。

回答by Jakub Nar?bski

In Git, branches are just pointers (references) to commits in a directed acyclic graph (DAG) of commits. This means that deleting a branch removes only references to commits, which might make some commits in the DAG unreachable, thus invisible. But all commits that were on a deleted branch would still be in the repository, at least until unreachable commits get pruned (e.g. using git gc).

在 Git 中,分支只是提交的有向无环图 (DAG) 中提交的指针(引用)。这意味着删除分支只会删除对提交的引用,这可能会使 DAG 中的某些提交无法访问,从而不可见。但是在已删除分支上的所有提交仍将在存储库中,至少直到无法访问的提交被修剪(例如使用git gc)。

Note that git branch -dwould refuse to delete a branch if it cannot be sure that deleting it wouldn't leave unreachable commits. You need to use the stronger git branch -Dto force deletion of a branch if it might leave unreachable commits.

请注意,git branch -d如果无法确定删除分支不会留下无法访问的提交,则将拒绝删除分支。如果git branch -D分支可能会留下无法访问的提交,则需要使用更强大的强制删除分支。

Note also that unreachable commits, if they are present, are only those commits between the last tip of a deleted branch and either a commit that got merged to another existing branch, any tagged commit, or the branching point; whichever is later. For example in the following situation:

另请注意,无法访问的提交(如果存在)仅是已删除分支的最后一个提示与合并到另一个现有分支的提交、任何标记的提交或分支点之间的那些提交;以较晚者为准。例如在以下情况下:

----O----*----*----/M----*    <-- master <-- HEAD
     \            /
      \--.----.--/--x---y     <-- deleted branch

only commits 'x' and 'y' would become unreachable after deleting the branch.

删除分支后,仅提交 'x' 和 'y' 将变得无法访问。

If you operated on a deleted branch within the gc.reflogExpireperiod, default 90 days, you would have the last tip of a deleted branch recorded in HEAD reflog (see git reflog show HEAD, or git log --oneline --walk-reflogs HEAD). You should be able to use HEAD reflog to recover the deleted pointer. Note also that in this case, unreachable commits in just a deleted branch would be protected from pruning (removing) within the gc.reflogExpireUnreachableperiod, which by default is 30 days.

如果您在gc.reflogExpire默认 90 天的时间段内对已删除的分支进行操作,您将在 HEAD reflog 中记录已删除分支的最后一个提示(请参阅git reflog show HEAD、 或git log --oneline --walk-reflogs HEAD)。您应该能够使用 HEAD reflog 来恢复已删除的指针。另请注意,在这种情况下,仅在已删除分支中无法访问的提交将在该gc.reflogExpireUnreachable期限内免受修剪(删除),默认情况下为 30 天。

If you can't find the tip of a just deleted branch in reflog for HEAD, you can try to use git fsckto find "unreachable commit <sha1>", and examine those (via git show <sha1>or git log <sha1>) to find the tip of the deleted branch.

如果在 HEAD 的 reflog 中找不到刚刚删除的分支的提示,您可以尝试使用git fsck查找“无法访问的提交 <sha1>”,并检查这些(通过git show <sha1>git log <sha1>)以找到已删除分支的提示。

Independent on how you find the tip of a deleted branch, you can undo deletion, or rather re-create a just deleted branch using

独立于您如何找到已删除分支的提示,您可以撤消删除,或者使用以下命令重新创建刚刚删除的分支

git branch <deleted-branch> <found-sha1-id>

Note however that reflog for a branch would be lost.

但是请注意,分支的 reflog 将丢失。



There is also git-resurrect.shscript in contrib/which helps find traces of a branch tip with given name and resurrect (undelete) it.

还有git-resurrect.sh脚本,contrib/它可以帮助找到具有给定名称的分支提示的痕迹并重新激活(取消删除)它。

回答by Johanned Nicolai

If you are worried about accidentally deleted branches and do not have a local copy of your repo any longer, there are extensions to enterprise Git servers like Gerrit that will detect history rewrites and branch deletions, will back them up under a special ref so that they can be restored if needed and will not be pruned by garbage collection. Gerrit administrators can still remove selected commits if needed for legal reasons.

如果您担心意外删除的分支并且不再有您的 repo 的本地副本,那么 Gerrit 等企业 Git 服务器的扩展将检测历史重写和分支删除,将它们备份到一个特殊的 ref 下,以便它们如果需要,可以恢复并且不会被垃圾收集修剪。如果出于法律原因需要,Gerrit 管理员仍然可以删除选定的提交。