How to Diff between local uncommitted changes and origin

GitGit Diff

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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)

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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] 

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
QuestionChaitanyaView Question on Stackoverflow
Solution 1 - GitJJDView Answer on Stackoverflow
Solution 2 - GitNateView Answer on Stackoverflow
Solution 3 - GitpepestarView Answer on Stackoverflow
Solution 4 - GitCanerView Answer on Stackoverflow
Solution 5 - GitJeffView Answer on Stackoverflow