英文:
GIT/DIFF - Reverse patch between a moved local file and an old commit
问题
在某个时间点,有一个特定文件的提交需要被撤销。我想要创建一个撤销补丁,将旧的提交(哈希值为 #A1B2C3D4)和我的工作文件夹中的文件(位于主分支上)进行比较:
folderA/file.c
...以及另一个目录结构中的文件:
folderB/folderC/file.c
这里对我来说比较困难的是,该文件已经被移动到另一个目录结构中。如何使用 git/diff/gittools/bc 生成这样一个反向补丁,用于一个已被移动的文件?
我查阅了文档,但似乎 git revert 命令不是我想要的。
感谢您的帮助。
[编辑]
我尝试了以下命令:
git diff A1B2C3D4 HEAD -- folderA/file.c folderB/folderC/file.c
它返回了以下结果:
相似性指数 100%
从 folderA/file.c 重命名
从 folderB/folderC/file.c 重命名
英文:
There was a commit for a particular file, at some point that needs to be reversed
I want to create a revert patch between that old commit with hash #A1B2C3D4:
folderA/file.c
...and the file in my working folder (on main branch):
folderB/folderC/file.c
The difficulty here, for me is that the file has been moved to another directory structure
how does one use git/diff/gittools/bc to generate such a reverse patch to a file that was moved?
I checked the doc, but git revert command does not seem to do what I want
thanks for your help
[edit]
I tried
git diff A1B2C3D4 HEAD -- folderA/file.c folderB/folderC/file.c
it returned
Similarity index 100%
rename from folderA/file.c
rename from folderB/folderC/file.c
答案1
得分: 1
如果在旧版本和新版本中存在足够多的相同文本,Git 应该能够确定文件已被重命名。然后,您还会收到一个显示更改的补丁文本,使用以下命令:
git diff old-commit HEAD -- folderA/file.c folderB/folderC/file.c
这个命令指示 git diff
检查这两个文件。当然,一个路径在 HEAD
中不存在,另一个路径在 old-commit
中不存在。因此,通过在命令中添加 --
来告诉 git diff
:不,我确实指的是以下两个参数是文件路径(更准确地说是路径规范)。
英文:
If there is sufficient common text in the old version and the new version, Git should be able to determine that the file was renamed. Then you also receive a patch text the shows the changes with this command:
git diff old-commit HEAD -- folderA/file.c folderB/folderC/file.c
The command instructs git diff
to investigate the two files. Of course, one path is not present in HEAD
the other is not present it old-commit
. For this reason, add --
to the command to tell git diff
: no, really, I mean that the following two arguments are file paths (more correctly: pathspec).
答案2
得分: 1
你可以检出 A1B2C3D4
,将文件重命名为你现在的名称。然后,你可以比较你关心的两个提交以生成补丁:
git checkout A1B2C3D4
git mv some-file some-file-new-path
git commit -m "重命名文件"
git diff the-other-commit -- some-file-new-path > some-patch-revert.patch
git diff HEAD the-other-commit -- some-file-new-path > some-patch.patch
使用最适合你需求的补丁。
英文:
You could checkout A1B2C3D4
, rename the file there to the name that you have now. Then you can compare the 2 commits that you care about to generate the patch
git checkout A1B2C3D4
git mv some-file some-file-new-path
git commit -m "Renamed file"
git diff the-other-commit -- some-file-new-path > some-patch-revert.patch
git diff HEAD the-other-commit -- some-file-new-path > some-patch.patch
Use the patch that best suits your needs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论