diff current working copy of a file with another branch's committed copy

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

The following works for me:

git diff master:foo foo

In the past, it may have been:

git diff foo master:foo

Solution 2 - Git

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.

Solution 3 - Git

git difftool tag/branch filename

Solution 4 - Git

Also: git diff master..feature foo

Since git diff foo master:foo doesn't work on directories for me.

Solution 5 - Git

git diff mybranch master -- file

should also work

Solution 6 - Git

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.

Solution 7 - Git

What also works:

git diff master ./relative-path-to-foo

Solution 8 - Git

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:

  1. git diff master..feature /Path/to/my_file shows the diff between the commited versions of my_file on branch master and feature.
  2. git diff master -- /Path/to/my_file shows the difference between the working directory (the un-staged files) and the branch master of my_file.

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
QuestionDogbertView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitJordan BroughView Answer on Stackoverflow
Solution 3 - GitAdirView Answer on Stackoverflow
Solution 4 - GitArtBITView Answer on Stackoverflow
Solution 5 - GitNaoise GoldenView Answer on Stackoverflow
Solution 6 - GitktaView Answer on Stackoverflow
Solution 7 - GitVojtech KaiserView Answer on Stackoverflow
Solution 8 - GitMeysam SadeghiView Answer on Stackoverflow