git 如何仅删除git日志中间的特定提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41261474/
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
How to delete a only a specific commit in the middle of the git log
提问by Dilshan
I want only to delete a specific commit in the middle of a git log. It shouldn't be affected to above commits.
我只想删除 git 日志中间的特定提交。它不应该受到上述提交的影响。
回答by Lo?c Faure-Lacroix
If the commit were not pushed to a remote server, you can use git rebase
to modify the git history.
如果提交未推送到远程服务器,则可以使用git rebase
修改 git 历史记录。
You can do that:
你可以这样做:
git rebase -i commit-hash^
It will show a list of commits. The first line should be the commit you want to delete. Remove that line from the file you edited, save and close.
它将显示提交列表。第一行应该是您要删除的提交。从您编辑的文件中删除该行,保存并关闭。
If you did push to a remote server prior to do that, note that the commits after the commit you just deleted were rewritten based on the commit before the one you just deleted from the history. For that particular reason, all of the commits are changed because your history just diverged. So if you try to push that, it won't be fast-forward and you'll have to force the push.
如果您在此之前确实推送到远程服务器,请注意,您刚刚删除的提交之后的提交是根据您刚刚从历史记录中删除的提交之前的提交重写的。出于这个特殊原因,所有提交都被更改了,因为您的历史记录刚刚发生分歧。因此,如果您尝试推动它,它不会快进,您将不得不强制推动。
For that reason, if you're not familiar with git rebase
I suggest keeping a branch pointing to the branch you were modifying the history in case something goes wrong.
出于这个原因,如果您不熟悉,git rebase
我建议保留一个指向您正在修改历史记录的分支的分支,以防出现问题。
Also, if that particular commit is shared accross multiple branches, diverging the history of one branch will cause multiple problems as you won't be able to easily merge one branch from the old history to the branch in the new history. It will duplicate many commit and the commit you deleted in one branch won't be deleted in the others.
此外,如果该特定提交是跨多个分支共享的,那么将一个分支的历史分开会导致多个问题,因为您将无法轻松地将旧历史中的一个分支合并到新历史中的分支。它将重复许多提交,并且您在一个分支中删除的提交不会在其他分支中删除。
Think of it has modifying git history is like time travel. Any change at one point create a new parallel universe. Everything that was after the change in the past really is impacted by the change you did in the past.
想想修改git历史就像时间旅行。任何一点的变化都会创建一个新的平行宇宙。过去改变之后的一切都确实受到你过去所做改变的影响。
回答by Tim Biegeleisen
It shouldn't be affected to above commits.
它不应该受到上述提交的影响。
If you remove a commit from the middle of a branch, or really anywhere except possibly the HEAD of a branch, you will rewritethe history of that branch. This is because the tools you would use to effect this, namely git rebase -interactive
and git filter-branch
, would need to recommit everything which comes after the commit you removed. This is usually not desirable for a public shared branch because it forces everyone to take pretty draconian measures to keep up with that change.
如果您从分支的中间删除提交,或者实际上除了分支的 HEAD 之外的任何地方都删除了提交,您将重写该分支的历史记录。这是因为您将用于实现此目的的工具,即git rebase -interactive
and git filter-branch
,需要重新提交您删除的提交之后的所有内容。对于公共共享分支来说,这通常是不可取的,因为它迫使每个人都采取非常严厉的措施来跟上这种变化。
Rather, a safer bet for you would be to use git revert
:
相反,对您来说更安全的选择是使用git revert
:
git revert <SHA-1 of commit to delete>
This will add a newcommit on the top of your branch which effectively undoes everything which the middle commit did.
这将在您的分支顶部添加一个新提交,这有效地撤消了中间提交所做的一切。
By the way, if you reallywant to remove that commit, I would recommend doing an interactive rebase.
顺便说一句,如果您真的想删除该提交,我建议您进行交互式变基。