简体   繁体   English

如何还原上一次提交中的部分更改

[英]How to revert part of changes in the previous commit

In the previous git commit , I add a new file (lets call it C) and make some changes to file A and file B.在之前的git commit中,我添加了一个新文件(我们称之为 C)并对文件 A 和文件 B 进行了一些更改。

Is there a way to remove file C and also revert the change made to file A by git commit --amend ?有没有办法删除文件 C 并恢复git commit --amend对文件 A 所做的更改?

Sounds like you mostly want to revert the commit but keep changes to one file.听起来您最想恢复提交但保留对一个文件的更改。

First, do a git revert but leave the changes staged:首先,执行 git 还原,但保留更改:

git revert -n HEAD

Then unstage changes you don't want reverted (file B)然后取消暂存您不想还原的更改(文件 B)

git reset HEAD -- B

And discard unstaged changes:并丢弃未分级的更改:

git checkout --.

This should leave staged changes to revert changes to files A and C.这应该保留分阶段更改以恢复对文件 A 和 C 的更改。 Then you can commit as you wish (new commit or amending previous commit if it hasn't yet been pushed elsewhere).然后您可以根据需要提交(如果尚未在其他地方推送,则新提交或修改先前的提交)。

git revert docs git 还原文档

If not pushed如果不推

You can reset to the previous commit and attempt to do what you really intended again.您可以重置为之前的提交并尝试再次执行您真正想要的操作。

git reset HEAD~1
git add A B
git commit ...

If pushed如果推

Since you're asking about git commit --amend I assumed the changes are local only.既然你问的是git commit --amend我假设这些更改只是本地的。 If the changes are already in the remote repo (aka pushed) then it may be better to remove C in a separate commit.如果更改已经在远程仓库中(又名推送),那么最好在单独的提交中删除C

git rm C 
git commit -m "Removing C"

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

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