175

Let's say I cloned a repository and started modifying files. I know that if I have local uncommitted changes, I can do a diff as follows git diff test.txt and it will show me the difference between the current local HEAD and the modified, uncommitted changes in the file. If I commit those changes I can diff it against the original repository by using git diff master origin/master

But is there any way of diff'ing the local changes with the original repository on the server before committing locally? I tried various permutations of git diff --cached master origin/master with no luck.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Chaitanya
  • 5,203
  • 8
  • 36
  • 61
  • I wanted to know how much my file has changed from the last committed version on my local. The answer to my question was this question. Thanks! – Souvik Ghosh Jul 18 '17 at 14:09
  • I have the same question, and none of the answers so far have worked for me. – Sohail Si Jul 13 '23 at 12:47

5 Answers5

157

Given that the remote repository has been cached via git fetch it should be possible to compare against these commits. Try the following:

$ git fetch origin
$ git diff origin/master
JJD
  • 50,076
  • 60
  • 203
  • 339
  • 5
    Ah, excellent. The key was to leave the master out. I had tried a combination where I was doing a fetch before, but when I did `git diff master origin/master` it was still comparing against the committed version (obvious in retrospect). But leaving out the master now compares local changes against the fetched version. – Chaitanya Jul 17 '13 at 00:37
  • @Chaitanya Good I could help. Actually I find the different styles Git uses a bit irritating: once you have to write `origin master` and somewhere else `origin/master`. There is still work to do, IMHO. – JJD Jul 17 '13 at 07:55
  • This doesn't seem to work: `git diff origin/master` returns `fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.` master exists on origin though, eg `git fetch origin master` works fine, – mikemaccana Apr 08 '15 at 18:06
52

I know it's not an answer to the exact question asked, but I found this question looking to diff a file in a branch and a local uncommitted file and I figured I would share

Syntax:

git diff <commit-ish>:./ -- <path>

Examples:

git diff origin/master:./ -- README.md
git diff HEAD^:./ -- README.md
git diff stash@{0}:./ -- README.md
git diff 1A2B3C4D:./ -- README.md

(Thanks Eric Boehs for a way to not have to type the filename twice)

Nate
  • 12,963
  • 4
  • 59
  • 80
  • I like the general solution better than the narrow, specific one above. Mind sharing where you found that? – fbicknel Aug 26 '15 at 14:46
  • I can't seem to find the documentation on the details of `commit-ish` and colon separator. The docs on [git-diff](http://git-scm.com/docs/git-diff) don't seem to mention it. I've used it for so long that I don't remember where I first found it. Probably other people's examples on other commands and I just experimented with `git-diff`. I'm afraid [another answer on `commit-ish`](http://stackoverflow.com/a/23303550/761771) is the best I can come up with at the moment. – Nate Aug 26 '15 at 18:25
  • 3
    A nice addition to this is `git diff master:./ -- README.md`. That way you don't have to type `README.md` twice and you can add it to an alias easier. – Eric Boehs Nov 19 '15 at 17:48
  • Thanks @EricBoehs, That's a great suggestion. – Nate Nov 19 '15 at 19:36
  • 2
    That syntax is not working for me ... rather `git diff master: -- README.md` the `./` creates a `/dev/null` source rather than the remote branch. Using git version 2.19.0 – rustyDev Apr 24 '19 at 22:08
27

To see non-staged (non-added) changes to existing files

git diff

Note that this does not track new files. To see staged, non-commited changes

git diff --cached

alex
  • 5,467
  • 4
  • 33
  • 43
pepestar
  • 317
  • 3
  • 2
10

If you want to compare files visually you can use:

git difftool

It will start your diff app automatically for each changed file.

PS: If you did not set a diff app, you can do it like in the example below(I use Winmerge):

git config --global merge.tool winmerge
git config --replace --global mergetool.winmerge.cmd "\"C:\Program Files (x86)\WinMerge\WinMergeU.exe\" -e -u -dl \"Base\" -dr \"Mine\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""
git config --global mergetool.prompt false
Caner
  • 57,267
  • 35
  • 174
  • 180
2

Generally speaking, the following command can make it, which gets all the details about the diffs between branches (current branch vs. another one), including uncommitted changes:

$ git diff origin/master

It is different from the command below, which ignores the diffs for uncommitted changes:

$ git diff origin/master..develop

You can add some options to filter out the diffs:

$ git diff origin/master [--name-only] [--diff-filter=A] [<path>]

The option '--diff-filter=A' means to filter out added files from origin/master branch. However, if you have run git rm before that, you will have to firstly push the changes into git stash and then restore the git repo and apply the stashed changes later on. Otherwise, it doesn't show the proper diffs as expected.

So it helps to see the diffs with an option '--name-status' for status.

$ git diff origin/master [--name-status] 
Jeff
  • 126
  • 4