129

Before using pull, I want to check if there are any differences between my local and GitHub master.

How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shin
  • 31,901
  • 69
  • 184
  • 271

3 Answers3

258

git pull is really equivalent to running git fetch and then git merge. The git fetch updates your so-called "remote-tracking branches" - typically these are ones that look like origin/master, github/experiment, etc. that you see with git branch -r. These are like a cache of the state of branches in the remote repository that are updated when you do git fetch (or a successful git push).

So, suppose you've got a remote called origin that refers to your GitHub repository, you would do:

git fetch origin

... and then do:

git diff master origin/master

... in order to see the difference between your master, and the one on GitHub. If you're happy with those differences, you can merge them in with git merge origin/master, assuming master is your current branch.

Personally, I think that doing git fetch and git merge separately is generally a good idea.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 3
    if you have setup remote tracking then the commands get a little simpler: 'git fetch' to fetch, 'git diff ..@{u}' to see the differences. I can never keep origin, master, and origin/master straight, so the short commands help a lot – ChrisBob Feb 16 '17 at 14:06
  • 3
    I tried you method, and got no outputs from `git diff master origin/master` and its opposite, but `git status` tells me `Your branch is ahead of 'origin/master' by 4 commits.` What's going on? – Jason Dec 19 '17 at 14:39
  • If `git status` outputs `Your branch is ahead of 'origin/master' by 4 commits.`, you'll need to stage & commit the changes `git add . && git commit -m "Initial commit"` before running `git diff master origin/master` – Stephen Pham Oct 18 '20 at 05:12
48

If you're not interested in the details that git diff outputs, you can just run git cherry which will output a list of commits your remote tracking branch has ahead of your local branch.

For example:

git fetch origin
git cherry master origin/master

Will output something like:

+ 2642039b1a4c4d4345a0d02f79ccc3690e19d9b1
+ a4870f9fbde61d2d657e97b72b61f46d1fd265a9

It indicates that there are two commits in my remote tracking branch that haven't been merged into my local branch.

This also works the other way:

git cherry origin/master master

It will show you a list of local commits that you haven't pushed to your remote repository yet.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
braitsch
  • 14,906
  • 5
  • 42
  • 37
  • 2
    Thanks, just what i needed. Just to be clear on that: If git cherry result is empty, than there's nothing for cherry picking and my local repo is up to date, right!? – eyecatchUp May 12 '12 at 14:22
  • 4
    You can also use the verbose option `-v`, to show the commits messages. For example : `git cherry -v origin/master master` will output : `+ 2642039b1a4c4d4345a0d02f79ccc3690e19d9b1 Fixed bug` – Yoluk Jun 06 '16 at 13:34
13

And another useful command to do this (after git fetch) is:

git log origin/master ^master

This shows the commits that are in origin/master, but not in master.

You can also do it in the opposite way when doing git pull to check what commits will be submitted to the remote.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
peter pan gz
  • 331
  • 3
  • 5