首页 > 解决方案 > 如何撤消两次提交之间的更改?

问题描述

我想在两次提交之间恢复一个文件更改,

commit 1 hash: abcde1....
   some code changes

commit 2 hash : abcde2....
   some code changes

commit 3 hash : abcde3....
   some code changes

.....

我可以使用git checkout abcde3 -- file/to/restore并恢复到提交 3,但丢失了提交 1 更改,
我如何才能仅签出提交 2 更改?

标签: git

解决方案


您可以简单地使用git revert.

恢复提交 2,这将创建一个新的提交,取消由所述提交 2 引入的任何更改。

如果git revert(在提交级别运行,并且not对于单个文件)恢复了太多文件,您可以重置您不想恢复的文件

git revert --no-commit <commit hash> # Revert, don't commit it yet
git reset # Unstage everything
git add yourFilesToRevert # Add the file to revert
git commit -m "commit message"
git reset --hard # Undo changes from the part of the revert that we didn't commit

推荐阅读