Compare file to another file in another commit

I have moved some code from a file to another file, and applied little changes in it. I would like to have a diff to see those changes.

The problem seems pretty common to me, but I have never needed to do such a thing. I only know how to compare the same file with himself in a previous revision, or 2 different files in the same revision.

  • Git Renaming Issues
  • Setting git private repository project on jenkins server
  • Workflow that enforces code review and keeps the integration branch pristine (git, Stash, TeamCity)
  • Free GIT Server with Web GUI a la BitBucket/GitHub
  • Where to store differences between dev and production branch in git?
  • what are limitations of shallow clone
  • Are there git diff options to compare one file in the current revision to another file in another commit? Or any other way?

  • How to get rid of “refs/bisect/bad” branches in Git
  • How do I use vim as 'git log' editor?
  • Why Github doesn't show my contributions even though I added my email address?
  • How do revert a local branch back to how it is in github?
  • Are pull requests part of Git, or a feature of tools like GitHub, Gerrit and Atlassian Stash?
  • Git telling me to pull, then commit, then pull?
  • 2 Solutions collect form web for “Compare file to another file in another commit”

    I think this is what you are looking for. HEAD~3 can be replaced with the commit id.

    git diff HEAD~3:oldfile.xml file/in/another/path/newfile.xml

    A dirty but working solution

    Use git ls-tree to get the SHA1 identifier of the files on the commits you want to compare then use git diff [options] <blob> <blob> and provide the two SHA1 identifiers as arguments.

    $ git ls-tree commit1 dir1/dir2/file1.txt
    100644 blob 7d252b754d46a8fcd0613a96710c9326942d7a92   dir1/dir2/file1.txt
    $ git ls-tree commit2 dirA/fileB.txt
    100644 blob 4d000ed739c880a26686a2843dae6eeeb4109a37   dirA/fileB.txt
    $ git diff 7d252b754d46a8fcd0613a96710c9326942d7a92 4d000ed739c880a26686a2843dae6eeeb4109a37

    If you need to do this frequently you can even pack everything in a small shell script:

    # This script does not validate its command line arguments!
    BLOB1=$(git ls-tree $COMMIT1 $FILE1 | cut -f3 -d' ' | cut -f1)
    BLOB2=$(git ls-tree $COMMIT2 $FILE2 | cut -f3 -d' ' | cut -f1)
    git diff $BLOB1 $BLOB2

    Run it as:

    $ commit1 dir1/dir2/file1.txt commit2 dirA/fileB.txt
    Git Baby is a git and github fan, let's start git clone.