1

What I want to to is to view the changes committed to my local repo against the remote one.

When I do git status I'm getting:

On branch develop  
Your branch is ahead of 'origin/develop' by 6 commits.  
  (use "git push" to publish your local commits)  
  nothing to commit, working directory clean  

As discussed in How do I show the changes which have been staged?

One (two) of the git diffs should work. Unfortunately in my case all of them return nothing.

git diff //empty - which seems to be fine 
git diff --cached //empty 
git diff HEAD //empty

Am I missing something?

abarger
  • 599
  • 7
  • 21
  • since you have already committed all your previously staged changes, what would you like to see? against what should `git diff` compare? – umläute Sep 30 '15 at 08:44

4 Answers4

2

You have already committed your changes.

Diff will show you the difference between your index and the repository.

If you want to view the changes between your local repository to the remote repository do this:

git fetch --all --prune
git log ^master origin/master

// or:
git log master ^origin/master

The difference between the two lines is that one will display the pull diff while the second one will display the push diff.

umläute
  • 28,885
  • 9
  • 68
  • 122
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
2

You have tried three forms of git diff: no options, --cached, and specifying a single revision (in this case, HEAD). The second answer in the question you've linked to shows a nice graphic summary of what these three forms of git diff do. None of these are what you want.

There are, however, more forms!

You can ask git diff to compare two specific commits (revisions), e.g.:

git diff HEAD@{upstream} HEAD

or:

git diff origin/develop develop

Based on your quoted git status output, these will do the same thing, as HEAD is currently the same as develop (you're "on branch develop" in the git status output), and HEAD@{upstream} will resolve to origin/develop ("ahead of 'origin/develop' by 6 commits"). (Note that in some shells/command-line-interpreters, you may have to quote one or both of the curly braces.)

You can also use:

git diff HEAD@{upstream}..HEAD

This notation, with the literal two-dots .., usually means something else: see the gitrevisions documentation for details. When using git diff, however, the front end command just makes this resolve to the two specified commits and then uses those two.


You'll get quite a bit more—and more typically what you might want, I think—with:

git log -p HEAD@{upstream}..HEAD

as this will show each commit separately, along with the changes made by those commits. Git finds these changes by comparing each commit with its parent commit. (This is also how git figures out which commits are those six. This is described, though in rather graph-theoretical form, in the "SPECIFYING RANGES" section.) In essence, git log -p looks at each commit to be shown, and then does a git diff between that commit's parent, and that commit.

For merge commits, the output of git log -p (and the output of git show, which is like git log -p but only shows the specific commit, or other object, you ask it to show, rather than walking the commit history like git log) is modified: git shows a "combined diff" by default, which tries to highlight what happened in the merge process, rather than what happened in the two branches. You don't need to know much about this now, just remember that when logging or showing a merge, git hides unwanted complexity (which is great until you want it; then search StackOverflow :-) ).

If you leave out the -p, git log will just show the commit messages. See the git log documentation for (many) more options.


As codeWizard noted above, all of these commands use git's idea of "where the remote is" based on the last time git called up the remote and picked up the newest information. This information can be out of date, possibly even by several seconds, or even minutes or (gasp) hours. :-) To get your local git up-to-date with the remote, you must run git fetch (the extra flags, --all and --prune, are not always needed although they may be a good idea depending on who you're sharing with and how). Run git fetch as often, or as rarely, as you like; remember that you could be seconds, or months, out of date, and git won't care either way: how often you want to update is for you to decide.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
0

This may come too late. But if you're using OS X, and your git core.pager or LESS environment variable has -F option. Sometimes git diff shows nothing.

(The git program is very solid, it's unlikely that you'd encounter a bug of git. This seems a bug of /usr/bin/less on OS X.)

ctchuang
  • 11
  • 2
-1

You have commits that are not present in a remote repository. git diff is not a command that gives you any information on this.

Git diff shows you not yet committed changes to your local repository. You do not get anything in your diff because of that reason. You have no changes to your local repository.

Git knows a couple of "stages"

  1. A change to a local repository. You have a file to which you made some changes compared to the committed version. You see these changes with git diff
  2. A staged change, ready to commit to a local repository. This is a set of changes that is staged to be committed. This is what will be committed if you git commit and is brought in this stage by git add. You see these changes with git diff --cached
  3. Committed change. This is now (at least locally) the new status quo of a file in your local repository. You can push this to a remote repository if you want.
hoppa
  • 3,011
  • 18
  • 21
  • "upstream"? git doesn't have a concept of *upstream* – umläute Sep 30 '15 at 08:35
  • fair enough, although you and I both know that in a lot of processes one refers to a specific remote repository as upstream (especially in open source software development). Still you are right, I corrected the usage of this incorrect term. – hoppa Sep 30 '15 at 08:38
  • `git diff` doesn't work on remotes. ever. (it *can* work on your *local checkout* of a remote if you explicitely tell it to do so e.g. `git diff origin/master`). So you might want to remove your first line, as it is misleading at best. – umläute Sep 30 '15 at 08:42
  • That is true, I'm not saying anything on the contrary. I state the OP has commits that are not present in a remote repository, since that is what the notice triggering the confusion is informing us of. Than I proceed to explain what git diff is actually used for. That is all. You are more than welcome to help me give better answers, truly you are. But I could do with less passive agressiveness :) I will edit the post again to be more clear on this as well though. – hoppa Sep 30 '15 at 08:46