How to diff one file to an arbitrary version in Git?

前端 未结 13 2006
青春惊慌失措
青春惊慌失措 2020-12-07 07:12

How can I diff a file, say pom.xml, from the master branch to an arbitrary older version in Git?

相关标签:
13条回答
  • 2020-12-07 07:17
    git diff master~20 -- pom.xml
    

    Works if you are not in master branch too.

    0 讨论(0)
  • 2020-12-07 07:18

    For comparing to 5 commit to the current one, both on master, just simply do:

    git diff master~5:pom.xml master:pom.xml
    

    Also you can refer to commit hash number, for example if the hash number is x110bd64, you can do something like this to see the difference:

    git diff x110bd64 pom.xml
    
    0 讨论(0)
  • 2020-12-07 07:19

    git diff -w HEAD origin/master path/to/file

    0 讨论(0)
  • 2020-12-07 07:23

    If you are looking for the diff on a specific commit and you want to use the github UI instead of the command line (say you want to link it to other folks), you can do:

    https://github.com/<org>/<repo>/commit/<commit-sha>/<path-to-file>
    

    For example:

    https://github.com/grails/grails-core/commit/02942c5b4d832b856fbc04c186f1d02416895a7e/grails-test-suite-uber/build.gradle

    Note the Previous and Next links at the top right that allow you to navigate through all the files in the commit.

    This only works for a specific commit though, not for comparing between any two arbitrary versions.

    0 讨论(0)
  • 2020-12-07 07:24

    If you need to diff on a single file in a stash for example you can do

    git diff stash@{0} -- path/to/file
    
    0 讨论(0)
  • 2020-12-07 07:27

    You can do:

    git diff master~20:pom.xml pom.xml
    

    ... to compare your current pom.xml to the one from master 20 revisions ago through the first parent. You can replace master~20, of course, with the object name (SHA1sum) of a commit or any of the many other ways of specifying a revision.

    Note that this is actually comparing the old pom.xml to the version in your working tree, not the version committed in master. If you want that, then you can do the following instead:

    git diff master~20:pom.xml master:pom.xml
    
    0 讨论(0)
提交回复
热议问题