tags:

views:

43

answers:

2

I'm new to git, using it via Terminal on Snow Leopard.

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?

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.

+1  A: 

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.

Jefromi
VonC's linked previous post says essentially the same thing as this, but the explanation's a bit different, so I'll leave this for now. (It also uses `@{1}` as a shorthand for `HEAD@{1}`)
Jefromi
@Jefromi: true, but I also like the explanation. +1
VonC
+1  A: 

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
Charles Bailey
Nice use of `git log` instead of `git diff` here (even if the syntax is a bit incoherent between the '..' for `git log` and the '...' for `git diff` ;) +1 See http://stackoverflow.com/questions/53569/how-to-get-the-changes-on-a-branch-in-git/53573#53573 and http://stackoverflow.com/questions/850607/difference-in-git-log-origin-master-vs-git-log-origin-master/850695#850695
VonC
Fortunately if you use the '..' syntax in a git diff command git "does the right thing".
Charles Bailey