175

I have cloned a github repository and made no changes locally. Github repository moved forward with commits on the same branch.

  1. How do I find a diff between my local repository and the original github repository?
  2. How do I find a diff between my working copy and the original github repository?
  3. How do I find a diff between my local repository and another github repository of the same project?
VividD
  • 10,456
  • 6
  • 64
  • 111
Delta George
  • 2,560
  • 2
  • 17
  • 11
  • 1
    possible duplicate of [compare local git branch with remote branch?](http://stackoverflow.com/questions/1800783/compare-local-git-branch-with-remote-branch) – Ciro Santilli OurBigBook.com Aug 30 '14 at 18:24
  • 1
    @CiroSantilli新疆改造中心六四事件法轮功: I think it's sufficiently different: the other question asks about any generic remote branch, whereas this is with reference to GitHub, plus asks about three different starting points. They're definitely similar, so your link is certainly useful. – jvriesem Sep 05 '18 at 19:50

3 Answers3

163

1) Add any remote repositories you want to compare:

git remote add foobar git://github.com/user/foobar.git

2) Update your local copy of a remote:

git fetch foobar

Fetch won't change your working copy.

3) Compare any branch from your local repository to any remote you've added:

git diff master foobar/master
dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • Will I need git fetch before diff? I guess there is no way to diff without it. – Delta George Mar 02 '11 at 03:17
  • This is actually a reply to question 3 only (diff with another github repo). – Ruslan Kabalin Mar 02 '11 at 10:44
  • 5
    Nothing distinguishes the "original github repository" from any other remote repository. Or am I misunderstanding something? – dbyrne Mar 02 '11 at 14:16
  • When I tried it with git 2.7.4 the "git diff master foobar/master" did not show differences. I looks to me like it compares my local copy ("master") with the repo given as 1st argument ("master") and in there only diffs the "path/file" "foobar/master". But the command "diff foobar/master" worked for me, comparing my local master with foobar/master. – Thomas Schütt Dec 14 '16 at 09:33
  • Instead of `fetch` (nr. 2) as an alternative: `git remote update` - it will update **all** of your branches set to track remote ones, but not merge any changes in; or `git pull` – Timo Oct 09 '17 at 20:51
  • @ Ruslan Kabalin : no. – Timo Oct 09 '17 at 20:54
51

Another reply to your questions (assuming you are on master and already did "git fetch origin" to make you repo aware about remote changes):

1) Commits on remote branch since when local branch was created:

git diff HEAD...origin/master

2) I assume by "working copy" you mean your local branch with some local commits that are not yet on remote. To see the differences of what you have on your local branch but that does not exist on remote branch run:

git diff origin/master...HEAD

3) See the answer by dbyrne.

Community
  • 1
  • 1
Ruslan Kabalin
  • 6,580
  • 2
  • 28
  • 20
  • Thank you for the `HEAD..origin/master` syntax! We've been getting errors with origin/HEAD not existing, and this solved it. – Dan Bechard Nov 06 '15 at 19:37
  • @ruslan: what does it mean when `git diff HEAD...origin/master` returns nothing if I have cloned a remote directory I am authorized to make changes to? – Mona Jalal Jul 06 '16 at 02:07
  • An error happened when I was cloning using Windows GUI hence I wonder if the clone went through completely. I see the folders are here in my directory but I want to make sure it's the same as remote. However in git shell , git diff returns nothing. I am confused if my clone has been successful or not? – Mona Jalal Jul 06 '16 at 02:09
  • @MonaJalal it means that there are no upstream changes since you cloned it. – Ruslan Kabalin Jul 06 '16 at 09:53
22

This example might help someone:

Note "origin" is my alias for remote "What is on Github"
Note "mybranch" is my alias for my branch "what is local" that I'm syncing with github
--your branch name is 'master' if you didn't create one. However, I'm using the different name mybranch to show where the branch name parameter is used.


What exactly are my remote repos on github?

$ git remote -v
origin  https://github.com/flipmcf/Playground.git (fetch)
origin  https://github.com/flipmcf/Playground.git (push)

Add the "other github repository of the same code" - we call this a fork:

$ git remote add someOtherRepo https://github.com/otherUser/Playground.git

$git remote -v
origin  https://github.com/flipmcf/Playground.git (fetch)
origin  https://github.com/flipmcf/Playground.git (push)
someOtherRepo https://github.com/otherUser/Playground.git (push)
someOtherRepo https://github.com/otherUser/Playground.git (fetch)

make sure our local repo is up to date:

$ git fetch

Change some stuff locally. let's say file ./foo/bar.py

$ git status
# On branch mybranch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   foo/bar.py

Review my uncommitted changes

$ git diff mybranch
diff --git a/playground/foo/bar.py b/playground/foo/bar.py
index b4fb1be..516323b 100655
--- a/playground/foo/bar.py
+++ b/playground/foo/bar.py
@@ -1,27 +1,29 @@
- This line is wrong
+ This line is fixed now - yea!
+ And I added this line too.

Commit locally.

$ git commit foo/bar.py -m"I changed stuff"
[myfork 9f31ff7] I changed stuff
1 files changed, 2 insertions(+), 1 deletions(-)

Now, I'm different than my remote (on github)

$ git status
# On branch mybranch
# Your branch is ahead of 'origin/mybranch' by 1 commit.
#
nothing to commit (working directory clean)

Diff this with remote - your fork: (this is frequently done with git diff master origin)

$ git diff mybranch origin
diff --git a/playground/foo/bar.py b/playground/foo/bar.py
index 516323b..b4fb1be 100655
--- a/playground/foo/bar.py
+++ b/playground/foo/bar.py
@@ -1,27 +1,29 @@
- This line is wrong
+ This line is fixed now - yea!
+ And I added this line too.

(git push to apply these to remote)

How does my remote branch differ from the remote master branch?

$ git diff origin/mybranch origin/master

How does my local stuff differ from the remote master branch?

$ git diff origin/master

How does my stuff differ from someone else's fork, master branch of the same repo?

$git diff mybranch someOtherRepo/master
FlipMcF
  • 12,636
  • 2
  • 35
  • 44
  • 5
    Before we do a 'git diff', I think it is necessary to do a 'git fetch' to make sure our local copy of the remote is up to date. – Don Nov 25 '13 at 23:08
  • 1
    I've read maybe 20 posts on how to compare remote and local I just figured this out myself: git fetch is REQUIRED first. git is truly the assembly code of RCS. jesus. Thanks for confirming, Don! – Terence Parr Sep 13 '14 at 18:50
  • just added `git fetch` to this answer. – FlipMcF Sep 15 '14 at 20:25
  • 1
    Hummm... I don't like how I used 'myfork' as my branch name. That could confuse someone (Like me, who just came back for this answer) – FlipMcF Apr 15 '15 at 15:36
  • Edited - clarity and answering the 'fork difference' question #3 – FlipMcF Nov 01 '16 at 17:09