如何将文件夹恢复到特定提交

发布于 2024-11-09 20:10:46 字数 355 浏览 5 评论 0原文

这是我的文件夹“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎夜雨 2024-11-16 20:10:46

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

git checkout e095 -- somefolder

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

git diff 89cd..e095 -- somefolder

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

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:

git diff 89cd..e095 -- somefolder
泅渡 2024-11-16 20:10:46

您可以使用 git reset 来重置索引,这还包括删除在最近提交中添加的文件(git checkout 本身不会执行此操作):

git reset e095 -- somefolder

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

git checkout -- somefolder

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

git clean -fd somefolder

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):

git reset e095 -- somefolder

However git reset doesn't update the working copy and the --hard option doesn't work with folders. So then use git checkout to make the working copy the same as the index:

git checkout -- somefolder

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

git clean -fd somefolder
看海 2024-11-16 20:10:46

git Restore 命令是将文件和目录恢复到不同提交时的状态的命令。如果您指定要恢复的目录,它还会删除您要从中恢复的提交中不存在的文件。

要将目录 somefolder 的内容恢复到提交 e095 时的状态,您可以运行:

git restore -s e095 somefolder

默认情况下,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 commit e095, you can run:

git restore -s e095 somefolder

By default, git restore restores content to the state it was in HEAD, but by using the -s switch (short for --source), we can restore content to the state it was in any commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文