如何在git的旧提交中恢复特定文件 [英] How to revert a specific file in a old commit on git

查看:121
本文介绍了如何在git的旧提交中恢复特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的问题已经接近这个一个,但我使用git。



所以,假设我有

Commit1 已更改 file1.c file2.c
$ b

Commit2 改变了 file2.c file3.c file4.c



Commit3 已更改 file1.c file2.c file3.c

等等...



然后,我只想恢复 Commit2 file2.c ,但是,请尽量保留 Commit3 在此文件中所做的更改...



认为?有什么方法可以做到这一点?



谢谢。

解决方案

您可以尝试恢复Commit2(即,应用新的 git commit,其改变与Commit2中所做的更改相反),但不提交。例如,在 gitk 中有一个即时菜单条目。在这个新的提交中,您只保留对 file2 进行的更改并提交结果。 另外,您可以简单地执行像

  git diff Commit2..Commit1  -  file2.c> revert.patch 
git apply revert.patch

查看一下该补丁在应用之前因Commit2之后的提交可能与Commit2中对 file2.c 的更改产生一些冲突。


I think my question is close to this one, but I'm using git.

So, let's say I have

Commit1 changed file1.c and file2.c

Commit2 changed file2.c, file3.c, file4.c

Commit3 changed file1.c , file2.c and file3.c

and so on...

Then, I would like to revert only the changes that Commit2 made in file2.c, but, try to keep the changes that Commit3 made in this file...

What you think? There is some way to do it?

Thank you.

解决方案

You may try to revert Commit2 (that is, apply a new git commit, whose changes would be opposite to the changes made in Commit2) but without committing. There's a instant menu entry in gitk for example. In this new commit you leave only changes made to file2 and commit the result.

Alternatively, you could simply do something like

git diff Commit2..Commit1 -- file2.c > revert.patch
git apply revert.patch

it might be useful to take a look on the patch before applying it because commits after Commit2 may introduce some conflicts with the changes to file2.c from Commit2.

这篇关于如何在git的旧提交中恢复特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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