I have a repo with file foo
in the master branch. I switched to bar branch and made some changes to foo
. How can I now run a git diff
between this copy (which isn't committed yet) and the copy of the master branch?
8 Answers
The following works for me:
git diff master:foo foo
In the past, it may have been:
git diff foo master:foo
-
10Each time I see an answer of yours regarding
git diff
, I always think of stackoverflow.com/questions/5256249/git-diff-doesnt-show-enough/…– VonCFeb 2, 2012 at 14:12 -
@VonC: thanks, it's nice to know it was worth drawing those :) Incidentally, do you happen to understand the oddity I updated my answer with? Feb 2, 2012 at 15:16
-
2Did you try with a
--
in order to separate parameters from path arguments?git diff -- master:foo foo
– VonCFeb 2, 2012 at 16:13 -
1On 1.8,
git diff -- master:foo foo
doesn't work - it seems to treat the argmaster:foo
as a non-existent filename (and ignores it) instead of a file-in-a-branch. Try switching the last 2 args - if it worked, the diff comparison should be reversed, but the output doesn't change.– KelvinSep 11, 2014 at 20:24 -
9For me it's the opposite -- I can do
git diff master:foo foo
but not vice versa. I don't understand it either. With git 1.7.9.5 / Ubuntu 12.04 I can at least dogit diff -R master:foo foo
to get the diff I actually want. When I try that with msysgit 1.9.4 / Windows 7 x64 I getfatal: unable to read 0000000000000000000000000000000000000000
. Without-R
I get the same error message as you with git 1.7.9.5, but with 1.9.4 I getfatal: master:foo: no such path in the working tree
.– JMMNov 21, 2014 at 22:28
You're trying to compare your working tree with a particular branch name, so you want this:
git diff master -- foo
Which is from this form of git-diff (see the git-diff manpage)
git diff [--options] <commit> [--] [<path>...]
This form is to view the changes you have in your working tree
relative to the named <commit>. You can use HEAD to compare it with
the latest commit, or a branch name to compare with the tip of a
different branch.
FYI, there is also a --cached
(aka --staged
) option for viewing the diff of what you've staged, rather than everything in your working tree:
git diff [--options] --cached [<commit>] [--] [<path>...]
This form is to view the changes you staged for the next commit
relative to the named <commit>.
...
--staged is a synonym of --cached.
-
2Thanks. No idea why, but only this answer worked for me with git 1.8.1 in Linux.– srkingApr 30, 2014 at 21:38
-
Excellent, thank you. I got hung up on wanting to include the name of my current branch in the diff command, but I see that's not needed.– yoyoFeb 24, 2015 at 20:55
-
This worked for me a few months ago, but doesn't seem to be now. Currently I'm at git version 2.7.4 (Apple Git-66); I don't know what I had before.– hBrentJun 13, 2016 at 16:40
-
@hBrent this still works for me on OSX 10.11 with git 2.7.3. Here's a quick sample repo with instructions if that's helpful: github.com/jordan-brough/git-diff-test Jun 14, 2016 at 16:04
-
git difftool tag/branch filename
-
This is the only answer that worked for me for comparison between a local working copy of a file, and that file version in a remote repo (not "origin"). Thanks, Adir and Baz– MouseMay 6, 2018 at 1:44
-
git difftool
has no-v
option according to docs git-scm.com/docs/git-difftool. Did you mean-y
?– ks1322Sep 4, 2019 at 10:21
Also: git diff master..feature foo
Since git diff foo master:foo
doesn't work on directories for me.
git diff mybranch master -- file
should also work
-
6This only works for files committed to mybranch, not current working copy of the file.– yoyoFeb 24, 2015 at 20:56
-
-
This allows compare in both directions:
git diff deployment master -- file
andgit diff master deployment -- file
are work as expected with any 2 branches. Jun 30, 2020 at 10:18
To see local changes compare to your current branch
git diff .
To see local changed compare to any other existing branch
git diff <branch-name> .
To see changes of a particular file
git diff <branch-name> -- <file-path>
Make sure you run git fetch
at the beginning.
lets say you have a branch named master
and a branch named feature
, and you want to check a specific file called my_file
, then:
git diff master..feature /Path/to/my_file
shows the diff between the commited versions ofmy_file
on branchmaster
andfeature
.git diff master -- /Path/to/my_file
shows the difference between the working directory (the un-staged files) and the branchmaster
ofmy_file
.