恢复提交中对文件的更改
我只想恢复特定提交对给定文件所做的更改。
我可以使用 git revert 命令吗?
还有其他简单的方法吗?
I want to revert changes made by a particular commit to a given file only.
Can I use git revert command for that?
Any other simple way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我见过的最干净的方法是描述此处
与 VonC 的响应类似,但使用
git show
和git apply
。The cleanest way I've seen of doing this is described here
Similar to VonC's response but using
git show
andgit apply
.假设可以更改提交历史记录,以下是在早期提交中恢复单个文件中的更改的工作流程:
例如,您想要在提交中恢复 1 个文件 (
badfile.txt
) 中的更改aaa222
:在基础提交上重新建立基础,修改有问题的提交,&继续。
1) 开始交互式变基:
2) 通过将
pick
更改为e
(用于编辑),在编辑器中标记问题提交以进行编辑:3) 恢复对错误文件的更改:
4)添加更改&修改提交:
5)完成变基:
Assuming it is ok to change the commit history, here's a workflow to revert changes in a single file in an earlier commit:
For example, you want to revert changes in 1 file (
badfile.txt
) in commitaaa222
:Rebase on the base commit, amend the problem commit, & continue.
1) Start interactive rebase:
2) Mark the problem commit for edit in the editor by changing
pick
toe
(for edit):3) Revert changes to the bad file:
4) Add the changes & amend the commit:
5) Finish the rebase:
更简单:
然后
。
文件就消失了,提交哈希、消息等是相同的
Much simpler:
then
and then
the file is gone and commit hash, message, etc is the same.
git 恢复
适用于提交中的所有文件内容。对于单个文件,您可以编写脚本:(
来自 git-shell-scripts 实用程序://github.com/smtlaissezfaire" rel="noreferrer">smtlaissezfaire)
注意:
另一种方法是 如果您尚未提交当前的修改,请参见此处。
git checkout
对文件有一些选项,修改文件来自 HEAD,覆盖您的更改。Dropped.on.Caprica 提及在评论中:
git revert
is for all file contents within a commits.For a single file, you can script it:
(From the git-shell-scripts utilities from smtlaissezfaire)
Note:
another way is described here if you have yet to commit your current modification.
git checkout
has some options for a file, modifying the file from HEAD, overwriting your change.Dropped.on.Caprica mentions in the comments:
如果您想重置上次提交后对文件的更改,这就是我通常使用的方法。我认为这是最简单的解决方案。
请注意,该文件将被添加到暂存区域。
希望它有帮助:)
If you'd like to reset the changes on a file from your last commit, this is what I'm usually using. I think this is the simplest solution.
Please note that the file will be added to the staging area.
Hope it helps :)
我只需使用
--no-commit
选项来git-revert
,然后在最终提交之前删除您不想从索引中恢复的文件。下面的示例展示了如何轻松还原第二个最近提交中对 foo.c 的更改:第一个 git-reset“取消暂存”所有文件,以便我们可以仅添加回该文件我们想要恢复的文件。最后的 git-reset --hard 删除了我们不想保留的剩余文件恢复。
I would simply use the
--no-commit
option togit-revert
and then remove the files you don't want reverted from the index before finally committing it. Here's an example showing how to easily revert just the changes to foo.c in the second most recent commit:The first
git-reset
"unstages" all files, so that we can then add back just the one file we want reverted. The finalgit-reset --hard
gets rid of the remaining file reverts that we don't want to keep.上面的命令将从提交中取出文件,但它会反映在 git status 中。
上面的命令将恢复更改(因此您将获得与 HEAD 相同的文件)。
(通过
--amend
来修改提交。)这样,已经在提交中的文件将被删除并恢复。
应从进行提交的分支开始执行上述步骤。
The above command will take file out of commit, but it will reflect in
git status
.The above command will revert the changes (as a result you get file same as HEAD).
(Pass
--amend
to amend commit.)With this, the file which is already in the commit is removed and reverted.
The above steps should be followed from the the branch where the commit is made.
您可以按照以下过程操作:
git revert -n <*commit*>
(-n
恢复所有更改,但不会提交它们)
git add <*filename*>
(你想要恢复和提交的文件的名称)git commit -m 'reverted message'
(添加一条用于恢复的消息)使用您在恢复之前提交的更改进行更新
You can follow this procedure:
git revert -n <*commit*>
(-n
revert all the changes but won'tcommit them)
git add <*filename*>
(name of the file/s you want to revert & commit)git commit -m 'reverted message'
(add a message for reverting)updated with the changes you committed before the revert