how to revert someone commit but keep other one's commit?

时间:2017-04-10 01:51:28

标签: git

I had a branch that

A---->my commit B--->other commit C-->current D

I want to revert "my commit B", but other commits stay the same.

how can I achieve this?

what I want is we will still develop on this branch. the git history would be: A---->my commit B--->other commit C-->current D --> revert my commit B

change to another branch can't accept, cause this branch is shared with others

3 个答案:

答案 0 :(得分:3)

正确完成您所描述的内容的正确方法是使用git revert命令。

你会发现有问题的提交,检查你的分支的提示并执行这个命令,如果它只是你要恢复的一个提交:

git revert SHA-OF-COMMIT

如果有一个以上的提交,你想要恢复,你会这样做:

git revert SHA-OF-OLDEST-COMMIT..SHA-OF-NEWEST-COMMIT

如果要还原多个不顺序的提交(中间还有其他提交),请按多个步骤执行还原操作。

在任何一种情况下,都会创建新的提交,以反转您想要还原的提交所引入的更改。你不会丢失原始提交,它们仍然是历史的一部分,但它们引入的更改已经在历史记录中被删除了。

如果您的存储库是共享存储库,那么这是最安全的路由,因为它只创建新的提交。 git revert不是要使用的命令,如果你需要完全根除提交的所有痕迹,包括历史记录,那么你需要进行历史重写,但这似乎不适合你的问题。

提示:在开始确保不做任何需要大量清理的工作之前,在本地制作完整的工作文件夹和git存储库的完整副本。

答案 1 :(得分:-1)

git checkout -b corrected-branch A
git cherry-pick C
git cherry-pick D

Or

git checkout -b corrected-branch A
git cherry-pick B..D

Or

git checkout -b corrected-branch A
git cherry-pick D~2..D

答案 2 :(得分:-1)

Interactive Rebasing: git rebase A -i (-i for "interactive") will let you pick and choose which commits to keep and which to discard.

More info: How to run git rebase interactive mode to remove duplicate commits