gitgit-diff

How to compare a file on disk with a file from a different branch which does not exist on disk


I want to perform a comparison operation between a file which exists on disk and a file which no longer exists because I moved it.

Here's the situation.

I now want to compare ./doc/A.txt with the state of ./src/A.txt which exists on the branch origin/main. Of course, this file does not actually exist in the current working directory.

I think one possible way to do this might be to do the following:

(Edit: Nope - this doesn't work. Use diff ./src/A.txt ./doc/A.txt instead.)

Is there a way to do it "directly"?

Maybe something like

git diff ./src/A.txt@origin/main ./doc/A.txt

Solution

  • You can get what you want with:

    git show origin/main:./src/A.txt | diff - doc/A.txt
    

    or

    git diff origin/main:./src/A.txt doc/A.txt