在 Git 中选择性地恢复或签出对文件的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/771897/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:21:12  来源:igfitidea点击:

Selectively revert or checkout changes to a file in Git?

git

提问by 1800 INFORMATION

Is there a command that allows you to partially undo the changes to a file (or files) in the working directory?

是否有一个命令允许您部分撤消对工作目录中一个(或多个)文件的更改?

Suppose you have edited a file a lot but you realize that you want to undo some of the changes back to the committed state, but not the other changes.

假设您编辑了一个文件很多,但您意识到您想要撤消某些更改回到提交状态,而不是其他更改。

I'm envisioning an option for git checkoutthat works a lot like git add -p, i.e. it goes through the file hunk by hunk and asks if you want to keep it or not.

我正在设想一个选项,git checkout它的工作原理很像git add -p,即它逐个遍历文件并询问您是否要保留它。

采纳答案by CB Bailey

You could use

你可以用

git add -p <path>

to stage the chunks that you want to keep in a particular file, then

暂存要保留在特定文件中的块,然后

git checkout -- <path>

to discard the working tree changes that you didn't want to keep, by checking out the staged version of the file.

通过检查文件的暂存版本来丢弃您不想保留的工作树更改。

Finally, you can use

最后,您可以使用

git reset -- <path>

to revert the staged version of the file to the most recent committed version of the file to leave you with your changes unstaged.

将文件的暂存版本恢复为文件的最新提交版本,使您的更改保持未暂存状态。

回答by thisgeek

With git version >= 1.7.1 I can

使用 git 版本 >= 1.7.1 我可以

git checkout -p

I am not sure when this feature was introduced.

我不确定这个功能是什么时候引入的。

回答by Koraktor

git checkout $filereverts the state of the file $fileto the last committed state. I think you can use git checkout SHA-1 -- $fileto revert the file to the commit identified by SHA-1.

git checkout $file将文件状态恢复$file到最后提交的状态。我认为您可以使用git checkout SHA-1 -- $file将文件恢复为由 SHA-1 标识的提交。

回答by araqnid

How many commits do you need to go back and select from? If it's just one, maybe take a branch just before it, checkout the file you committed and then use git add -pto add it how you wanted it. Then you can go back to where you were and checkout the file from your temp branch.

您需要返回多少次提交并从中选择?如果它只是一个,也许在它之前取一个分支,检出你提交的文件,然后用git add -p你想要的方式添加它。然后您可以返回到您所在的位置并从您的临时分支签出该文件。

that is:

那是:

git checkout -b temp troublesome-commit^
git checkout troublesome-commit -- path/to/file
git add -p path/to/file
git commit -c troublesome-commit
git checkout @{-1}
git checkout temp -- path/to/file
git commit path/to/file
git branch -D temp

Other alternatives include going back and editing the commit with git rebase -i(marking the commit as edit, then doing a git reset HEAD^and redoing the commit when dropped back into the shell).

其他替代方法包括返回并编辑提交git rebase -i(将提交标记为edit,然后执行 agit reset HEAD^并在放回 shell 时重做提交)。

If the changes you need to select from are spread over a series of commits, it may be better to extract them as patches (or a patch covering all of them) and hand-edit the patch, taking out the changes you want to keep, and feeding the residual into git apply --reverse.

如果您需要从中选择的更改分布在一系列提交中,最好将它们提取为补丁(或一个覆盖所有补丁的补丁)并手动编辑补丁,删除您想要保留的更改,并将残渣喂入git apply --reverse