如何将文件夹恢复到特定提交
这是我的文件夹“somefolder”的历史记录,
$ git log somefolder
commit 89cd
More changes to somefolder
commit ef47a
Updating somefolder and other stuff
commit e095
Bugs fixed in somefolder
我想将某个文件夹恢复到“某个文件夹中修复的错误”提交。
由于第二次提交涉及某个文件夹之外的更改,所以我不想恢复此提交。
我想最安全的方法方法是在提交 e095 和 89cd 之间创建一个仅适用于某个文件夹的差异/补丁,然后应用该补丁,我该怎么做?
Here's my history for the folder '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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 git checkout 将存储库更新到特定状态。
至于你关于生成差异的问题,那也可以。只需生成差异即可从当前状态返回到
e095
:You can use git checkout to update your repository to a specific state.
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
:您可以使用 git reset 来重置索引,这还包括删除在最近提交中添加的文件(
git checkout 本身不会执行此操作):
但是
git reset
不会更新工作副本,并且--hard
选项不适用于文件夹。因此,然后使用 git checkout 使工作副本与索引相同:然后如果您还想删除添加的任何文件,您还需要执行以下操作:
You can use
git reset
to reset the index which will also include removing files that were added in more recent commits (git checkout
on it's own doesn't do this):However
git reset
doesn't update the working copy and the--hard
option doesn't work with folders. So then usegit checkout
to make the working copy the same as the index:and then if you also want to remove any files added you also need todo:
git Restore 命令是将文件和目录恢复到不同提交时的状态的命令。如果您指定要恢复的目录,它还会删除您要从中恢复的提交中不存在的文件。
要将目录
somefolder
的内容恢复到提交e095
时的状态,您可以运行:默认情况下,
git restore
将内容恢复到状态它在HEAD
中,但是通过使用-s
开关(--source
的缩写),我们可以将内容恢复到它的状态处于任何提交中。The
git restore
command is the command designed restore files and directories to the state they were in a different commit. If you specify a directory to restore, it will also remove files that were not present in the commit you are restoring from.To restore the content of directory
somefolder
to the state it was in commite095
, you can run:By default,
git restore
restores content to the state it was inHEAD
, but by using the-s
switch (short for--source
), we can restore content to the state it was in any commit.