GIT/DIFF - Reverse patch between a moved local file and an old commit

94 Views Asked by At

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
2

There are 2 best solutions below

4
eftshift0 On BEST ANSWER

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.

2
j6t On

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