git 如何通过创建补丁将文件夹恢复到特定提交

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

How to revert a folder to a particular commit by creating a patch

git

提问by hitfactory

Here's my history for the folder 'somefolder'

这是我的文件夹“somefolder”的历史记录

$ git log somefolder

commit 89cd
More changes to somefolder

commit ef47a
Updating somefolder and other stuff

commit e095
Bugs fixed in somefolder

I want to revert somefolder back to the 'Bugs fixed in some folder" commit.

我想将某个文件夹恢复到“某个文件夹中已修复的错误”提交。

Since the second commit involved changes outside of somefolder, I don't want to revert this commit.

由于第二次提交涉及某个文件夹之外的更改,因此我不想还原此提交。

I guess the safest way would be to create a diff/patch between commit e095 and 89cd that applies just to some folder, and then apply that patch. How can I do that?

我想最安全的方法是在提交 e095 和 89cd 之间创建一个仅适用于某个文件夹的差异/补丁,然后应用该补丁。我怎样才能做到这一点?

回答by jamessan

You can use git checkoutto update your repository to a specific state.

您可以使用git checkout将存储库更新到特定状态。

git checkout e095 -- somefolder

As for your question about generating the diff, that would work too. Just generate the diff to go from your current state back to e095:

至于你关于生成差异的问题,那也行。只需生成差异即可从当前状态返回到e095

git diff 89cd..e095 -- somefolder

回答by Matthew Buckett

You can use git resetto reset the index which will also include removing files that were added in more recent commits (git checkouton it's own doesn't do this):

您可以使用git reset重置索引,其中还包括删除在最近提交中添加的文件(git checkout它本身不会这样做):

git reset e095 -- somefolder

However git resetdoesn't update the working copy and the --hardoption doesn't work with folders. So then use git checkoutto make the working copy the same as the index:

但是git reset不会更新工作副本,并且该--hard选项不适用于文件夹。然后使用git checkout使工作副本与索引相同:

git checkout -- somefolder

and then if you also want to remove any files added you also need todo:

然后如果您还想删除添加的任何文件,您还需要执行以下操作:

git clean -fd somefolder