8

I'm a little confused about git fetch and comparing differences .

I have the following local branches;

  • master
  • remote/origin/master

In master branch I have a text file which I make changes to, commit and then push to the origin/master.

In another local repo (for test purposes) I have a remote to the same repo as above. I run

  • git fetch origin master
  • git diff master origin/master

It displays no differences, but if i do git pull origin master it pulls and merges the changes I made to the text file. I'm probably wrong but I thought a pull did a fetch and a merge, so doing a fetch allowed me to see the changes to the remote branch before merging them.

screenm0nkey
  • 18,405
  • 17
  • 57
  • 75
  • possible duplicate of [How to preview git-pull without doing fetch?](http://stackoverflow.com/questions/180272/how-to-preview-git-pull-without-doing-fetch) – richq Sep 30 '11 at 17:08

1 Answers1

11

What you need to do to perform a diff (after a fetch) in respect to the head of your branch and the origin at the same branch is a

git diff HEAD...origin

Please note the 3 dots. By the way, the question can possibly be considered a duplicate of this one, at least in terms of the accepted answer.

Community
  • 1
  • 1
Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
  • That didn't work unfortunately. if I do as you said I get fatal: ambiguous argument. so I did git diff HEAD...origin/master and it didn't list any differences. But when I do a pull it fetches the changes and merges them? – screenm0nkey Sep 30 '11 at 14:40
  • I'm at a loss here, really. It should work as advertised above. Just to be certain of your repo config, could you dump here your `git config -l --local`? – Luca Geretti Sep 30 '11 at 14:45
  • So I got it to work when I cloned the remote repo. The repo above which didn't work I set up by using 'git remote add' and then I checked out the remote branch so I'm clearling missing something when I set it up manually. I read something about trackig so maybe that's it. – screenm0nkey Sep 30 '11 at 14:52
  • 1
    Anyway, while the reason why `git diff HEAD...origin` does not work currently excapes me, `git diff HEAD...origin/master` **has** to work correctly. What does a `git status` return, by the way? You should have some feedback on being behind 'origin/master'. – Luca Geretti Sep 30 '11 at 14:55
  • I got it to work with the manually configured repo. I must have been doing the fetch wrong. I used 'git fetch origin master:remotes/origin/master' Thanks for your help. – screenm0nkey Sep 30 '11 at 15:05
  • Glad you made it. I am still curious as what config you got from your setup. The fetch above seems legit. – Luca Geretti Sep 30 '11 at 15:16