如何从单个文件中的旧提交还原更改 [英] How to revert changes from old commit in a single file

查看:59
本文介绍了如何从单个文件中的旧提交还原更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何还原/删除在较早的多文件提交中所做的更改,但仅在单个文件中执行?IE.像

How do I revert/remove the changes done in an older multi-file commit, but only do it in a single file? I.e. something like

git revert< commit-specifier>< file>

git revert 除外,不接受< file> 参数.以下答案均不能解决此问题:

except git revert does not accept <file> argument. None of the following answers addresses this problem:

Git:还原单个文件上的旧提交的意义在于如何调试冲突.

Git: Revert old commit on single file is rather about how to debug conflicts.

在已推送到远程存储库的Git 无法解决我的单个文件问题.

Undo a particular commit in Git that's been pushed to remote repos does not address my single file issue.

Git:在较旧的提交上还原也不能解决单个文件的问题./p>

Git: revert on older commit also does not address single file issue.

推荐答案

Git是一个工具集,而非解决方案,因此有多种解决方案.但是,一种相对简单的方法是从 git revert -n 开始,该命令开始还原,但没有完成

Git is a tool-set, not a solution, so there are multiple solutions. However, one relatively straightforward way is to start with git revert -n, which starts the revert but does not finish it:

git revert -n <commit-specifier>

这当然试图将对所有 all 文件的更改全部撤回.您只想撤消对一个文件的更改.但是,现在 git revert 进行了无需提交的尝试,您只需要恢复每个您不想更改的文件.获取此类文件的列表,然后使用 git checkout git restore (严格按照 git status 的建议使用命令)制作这些文件匹配当前提交.现在, git status 将仅显示一个文件作为要提交的更改,现在您可以 git commit 该文件.

This tries to back out all changes to all files, of course. You only want to back out changes to one file. But now that git revert has made this attempt without committing you merely need to restore each file that you didn't want changed. Get a list of such files, and then use git checkout or git restore—using the commands exactly as git status advises—to make those files match the current commit. Now git status will show only the one file as changes to be committed, and you can now git commit that one file.

另一种相对简单的方法是使用:

Another relatively straightforward way is to use:

git show <commit-specifier> -- <pathspec> | git apply -R

如果您希望Git在文件的基本版本上使用三向合并,则可以将 -3 添加到 git apply 命令中;在这种情况下,将-full-index 添加到 git show 命令选项中可能会有所帮助.(与cherry-pick -n 方法一样,您必须自己提交结果.)

You can add -3 to the git apply command if you'd like Git to use a three-way merge on the base version of the file; in this case it may help to add --full-index to the git show command options. (As with the cherry-pick -n method you will have to commit the result yourself.)

这篇关于如何从单个文件中的旧提交还原更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆