git: How to diff changed files versus previous versions after a pull?

When I run “git pull” I often want to know what changed between the last version of a file and the new one. Say I want to know what someone else committed to a particular file.

How is that done?

  • svn:externals a sub-folder of a git project
  • Can anyone explain what git cherry-pick <sha> does?
  • How to setup Git-plus in Atom to work with Github
  • Track but don't stage files with git?
  • How do I preserve tags when filtering a repository?
  • Git recovery: “object file is empty”. How to recreate trees?
  • I’m assuming it’s “git diff” with some parameters for commit x versus commit y but I can’t seem to get the syntax. I also find “git log” confusing a bit and am not sure where to get the commit ID of my latest version of the file versus the new one.

  • Commit part of a file with command line git (for use with a script)
  • “unqualified destination” error with git subtree push
  • HgGit: “invalid-email-address” at GitHub
  • Git error pushing to github using gitextensions
  • How to move files to folders in github?
  • Force Adding Submodule Contents in Git
  • 3 Solutions collect form web for “git: How to diff changed files versus previous versions after a pull?”

    There are all kinds of wonderful ways to specify commits – see the specifying revisions section of man git-rev-parse for more details. In this case, you probably want:

    git diff HEAD@{1}
    

    The @{1} means “the previous position of the ref I’ve specified”, so that evaluates to what you had checked out previously – just before the pull. You can tack HEAD on the end there if you also have some changes in your work tree and you don’t want to see the diffs for them.

    I’m not sure what you’re asking for with “the commit ID of my latest version of the file” – the commit “ID” (SHA1 hash) is that 40-character hex right at the top of every entry in the output of git log. It’s the hash for the entire commit, not for a given file. You don’t really ever need more – if you want to diff just one file across the pull, do

    git diff HEAD@{1} filename
    

    This is a general thing – if you want to know about the state of a file in a given commit, you specify the commit and the file, not an ID/hash specific to the file.

    I like to use:

    git diff HEAD^
    

    Or if I only want to diff a specific file:

    git diff HEAD^ -- /foo/bar/baz.txt
    

    If you do a straight git pull then you will either be ‘fast-forwarded’ or merge an unknown number of commits from the remote repository. This happens as one action though, so the last commit that you were at immediately before the pull will be the last entry in the reflog and can be accessed as HEAD@{1}. This means that you can do:

    git diff HEAD@{1}
    

    However, I would strongly recommend that if this is something you find yourself doing a lot then you should consider just doing a git fetch and examining the fetched branch before manually merging or rebasing onto it. E.g. if you’re on master and were going to pull in origin/master:

    git fetch
    
    git log HEAD..origin/master
    
     # looks good, lets merge
    
    git merge origin/master
    
    Git Baby is a git and github fan, let's start git clone.