Search code examples
gitgit-diff

Compare file between commits when folder structure has changed


Question:
How do I git diff a particular file when the folder structure has changed between commit_hash_1 and commit_hash_2?

What I know:
I can git diff a file between two commits with:

git diff commit_hash_1 commit_hash_2 folder/file.ext

Attempted Solutions:
1. My guess was something like:

git diff commit_hash_1:/folder/file.ext commit_hash_2:/new_folder/file.ext

But that doesn't work; it gives me:

fatal: Path '/new_folder/file.ext' does not exist in 'commit_hash_2'

2. So the other option is:

git diff commit_hash_1 commit_hash_2 new_folder/file.ext

But that starts a comparison between the commit_hash_2 file.ext and /dev/null.


Solution

  • The attempted solution is nearly correct, but those opening slashes are wrong. Try:

    git diff commit_hash_1:folder/file.ext commit_hash_2:new_folder/file.ext
    

    Thanks @dlsso for sending me on the right track.