[git] git: diff between file in local repo and origin

I want to find the differences between a file I have in my local repo vs what is in the origin master.

I know that there is git diff, however I just want to isolate it down to this one particular file.

For simplicity lets say the file is named file1.txt and it has a local file path = [local_path] and in the origin it has filepath = [remote-path].

What would be the git command I need to type?

EDIT: Thank you all for your input it has been very insightful. For those that are using Eclipse (which I am and I should have stated earlier) I just found out that you can just rightclick -> Compare With -> Branch, Tag or Reference -> select appropriate version and there you go.

This question is related to git git-diff

The answer is


To check with current branch:

git diff -- projects/components/some.component.ts ... origin
git diff -- projects/components/some.component.html ... origin

To check with some other branch say staging:

git diff -- projects/components/some.component.ts ... origin/staging
git diff -- projects/components/some.component.html ... origin/staging

For that I wrote a bash script:

#set -x 
branchname=`git branch | grep -F '*' |  awk '{print $2}'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | awk '{if ($1 == "modified:") print $2;}'`
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file 
git difftool  FETCH_HEAD $file ;
done

In the above script, I fetch the remote main branch (not necessary its master branch ANY branch) to FETCH_HEAD, then make a list of my modified file only and compare modified files to git difftool.

There are many difftool supported by git, I configured Meld Diff Viewer for good GUI comparison.
From the above script, I have prior knowledge what changes done by other teams in same file, before I follow git stages untrack-->staged-->commit which help me to avoid unnecessary resolve merge conflict with remote team or make new local branch and compare and merge on the main branch.


To compare local repository with remote one, simply use the below syntax:

git diff @{upstream}

I tried a couple of solution but I thing easy way like this (you are in the local folder):

#!/bin/bash
git fetch

var_local=`cat .git/refs/heads/master`
var_remote=`git log origin/master -1 | head -n1 | cut -d" " -f2`

if [ "$var_remote" = "$var_local" ]; then
    echo "Strings are equal." #1
else
    echo "Strings are not equal." #0 if you want
fi

Then you did compare local git and remote git last commit number....


To view the differences going from the remote file to the local file:

git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt

To view the differences in the other direction:

git diff HEAD:local/path/file1.txt remotename/branchname:remote/path/file1.txt

Basically you can diff any two files anywhere using this notation:

git diff ref1:path/to/file1 ref2:path/to/file2

As usual, ref1 and ref2 could be branch names, remotename/branchname, commit SHAs, etc.


Full answer to the original question that was talking about a possible different path on local and remote is below

  1. git fetch origin
  2. git diff master -- [local-path] origin/master -- [remote-path]

Assuming the local path is docs/file1.txt and remote path is docs2/file1.txt, use git diff master -- docs/file1.txt origin/master -- docs2/file1.txt

This is adapted from GitHub help page here and Code-Apprentice answer above