hasanghaforian
hasanghaforian

Reputation: 14032

git revert file to previous commit after changing it's path

I have a file ./new-path/file.java which is controlled by Git. In an earlier commit (<SHA-1>) it's path was ./older-path. Now I want to restore it's older content as was committed in <SHA-1>.

I can restore that file by doing git checkout <SHA-1> -- ./older-path/file.java and then copy it's content to ./new-path/file.java. But I have to do above steps for multiple older commits and this way is not appropriated. How I can do it directly by Git?

Upvotes: 3

Views: 40

Answers (1)

torek
torek

Reputation: 489173

You can't, but that's not actually a problem.

If you ask Git to check it out by name you will get the old name.

If you ask Git to show you the content, you do this by the old name, and the content goes to standard output, which you can redirect, but not using Git. So you can do it directly, but not directly by Git:

git show <hash>:./older-path/file.java > new-path/file.java

Note that git show will not apply filters (including end of line filtering). Adding --textconv is supposed to cause it to do such conversions, but this is not working in my experiments (with Git 2.19.0). An alternative is to use git cat-file --filters <hash>:<path>, which does seem to work. However, the filters used are those defined in the current .gitattributes rather than the .gitattributes associated with the specified commit.

Upvotes: 3

Related Questions