How to check the differences between a local and GitHub repository before the pull

GitGithub

Git Problem Overview


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

How can I do it?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshinView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitbraitschView Answer on Stackoverflow
Solution 3 - Gitpeter pan gzView Answer on Stackoverflow