简体   繁体   English

如何恢复 git commit 但保留非常原始的 git commit 历史(在 git blame 中)

[英]How can I revert a git commit but preserve the very original git commit history (in git blame)

I am going to revert a problematic commit but after the revert if I run the git blame on the file I am seeing my revert as the most recent commit that touched those lines.我将还原一个有问题的提交,但是在还原之后,如果我对文件运行 git blame ,我会看到我的还原是触及这些行的最新提交。 That makes sense but not very useful since I want to see the git history of the original file.这是有道理的,但不是很有用,因为我想查看原始文件的 git 历史记录。 (ie just like the problematic commit and the revert never happened). (即就像有问题的提交和恢复从未发生过一样)。

Is there a way to do that?有没有办法做到这一点?

Thanks!谢谢!

If you want to "remove" a commit from the history of a branch then you have to: - git checkout the previous revision - git cherry-pick revisions you want to keep that are after the revision you want to remove如果你想从一个分支的历史中“删除”一个提交,那么你必须: - git checkout 上一个修订版 - 你想要保留的 git cherry-pick 修订版是你想删除的修订版之后

Say you wanted to remove branch-name~3 from the history of a branch:假设您想从分支的历史记录中删除 branch-name~3:

git cherry-pick branch-name~4 # (this is a keeper)
git cherry-pick branch-name~3..branch-name # apply revisions after branch-name~3

Voila.瞧。 It never happened in the first place.它从来没有发生过。 In your case, exclude the revert revision when cherry-picking and you are done.在您的情况下,在挑选时排除恢复修订,您就完成了。

A revert is no different from a new commit with a reversed diff.还原与具有反向差异的新提交没有什么不同。 So, no, you can't make git-blame ignore the original commit or the revert (unless you are willing to change history, in which case you aren't actually reverting anything at all).所以,不,你不能让git-blame忽略原始提交或恢复(除非你愿意改变历史,在这种情况下你实际上根本没有恢复任何东西)。

However, because it is just like any other commit, you can do just what you would with any other commit, if you see that the commit that last touched some lines was not useful, and ask git-blame to annotate a previous version of the file.但是,因为它就像任何其他提交一样,如果您发现最后触及某些行的提交没有用,并且要求git-blame注释先前版本的文件。

For example, suppose that you ran例如,假设你跑了

$ git blame HEAD -- foo.c

and saw:并看到:

7507efdad27 foo.c (amalloy) ...

on the line you are interested in. You decide this commit isn't useful, because it was a revert.在您感兴趣的行上。您认为此提交没有用,因为它是一个还原。 You can check which commit was reverted by that commit, with git show 7507efdad27 .您可以使用git show 7507efdad27检查该提交还原了哪个提交。

Suppose that yields 673bdae7548 : You can then ask for a blame view of the file before that commit:假设产生673bdae7548 :然后您可以在提交之前请求文件的blame视图:

$ git blame 673bdae7548^ -- foo.c

Which will show you the blame before the commit that was reverted, so the lines you're interested in will be annotated with the commit that last touched them before the commit that was reverted.这将向您显示还原提交之前的责任,因此您感兴趣的行将使用在还原提交之前最后接触它们的提交进行注释。

Run your blame from the revert's parent, see the git blame docs;从还原的父级运行您的责任,请参阅 git blame 文档; you can even specify an explicit list of commits you're interested in checking and just delete the revert from an ordinary rev-list's output.您甚至可以指定您有兴趣检查的显式提交列表,然后从普通 rev-list 的输出中删除还原。

If you don't want to edit your history and you don't mind an extra commit you can do this by:如果您不想编辑历史记录并且不介意额外提交,则可以通过以下方式执行此操作:

  1. Go back to a commit before the changes which have been reverted.返回到已还原更改之前的提交。
  2. Create a branch, and merge your branch into it with the option --no-ff (no fast forward)创建一个分支,并使用选项 --no-ff(无快进)将您的分支合并到其中

If you blame the new branch you should now see the original author for the lines which was reverted.如果你责怪新分支,你现在应该看到被还原的行的原作者。


Just a note, this should in theory automatically happen if you at some point merge your branch into a master/main making this unnecessary (assuming your don't ever merge into master/main with fast forward).请注意,理论上这应该会自动发生,如果您在某个时候将您的分支合并到一个主/主中,从而使这变得不必要(假设您永远不会以快进的方式合并到主/主中)。


I haven't tested how any of this works if you revert something from an older branch which already is merged into master/main.如果您从已经合并到 master/main 的旧分支中恢复某些内容,我还没有测试过这些是如何工作的。 My best guess would be that you would need to create the branch from step 2 at a version of master which didn't have the change which should be reverted.我最好的猜测是,您需要在没有应该恢复的更改的 master 版本上从第 2 步创建分支。 it might just work ¯\\ (ツ) /¯.它可能只是工作¯\\ (ツ) /¯。

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

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