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

前端 未结 13 2007
青春惊慌失措
青春惊慌失措 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:27

    To see what was changed in a file in the last commit:

    git diff HEAD~1 path/to/file
    

    You can change the number (~1) to the n-th commit which you want to diff with.

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

    Generic Syntax :

    $git diff oldCommit..newCommit -- **FileName.xml > ~/diff.txt
    

    for all files named "FileName.xml" anywhere in your repo.

    Notice the space between "--" and "**"

    Answer for your question:

    $git checkout master
    $git diff oldCommit..HEAD -- **pom.xml 
    or
    $git diff oldCommit..HEAD -- relative/path/to/pom.xml 
    

    as always with git, you can use a tag/sha1/"HEAD^" to id a commit.

    Tested with git 1.9.1 on Ubuntu.

    0 讨论(0)
  • 2020-12-07 07:35
    git diff <revision> <path>
    

    For example:

    git diff b0d14a4 foobar.txt
    
    0 讨论(0)
  • 2020-12-07 07:35

    If neither commit is your HEAD then bash's brace expansion proves really useful, especially if your filenames are long, the example above:

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

    Would become

    git diff {master~20,master}:pom.xml
    

    More on Brace expansion with bash.

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

    If you want to see the difference between the last commit of a single file you can do:

    git log -p -1 filename
    

    This will give you the diff of the file in git, is not comparing your local file.

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

    For people interested in doing the same from GitHub, see comparing commits across time.

    0 讨论(0)
提交回复
热议问题