256

I want to find the differences between a file I have in my local repository 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, let’s 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?


For those that are using Eclipse, I just found out that you can just right click → Compare With → Branch, Tag or Reference → select the appropriate version and there you go.

codeforester
  • 39,467
  • 16
  • 112
  • 140
SeekingAlpha
  • 7,489
  • 12
  • 35
  • 44

8 Answers8

322

If [remote-path] and [local-path] are the same, you can do

$ git fetch origin master
$ git diff origin/master -- [local-path]

Note 1: The second command above will compare against the locally stored remote tracking branch. The fetch command is required to update the remote tracking branch to be in sync with the contents of the remote server. Alternatively, you can just do

$ git diff master:<path-or-file-name>

Note 2: master can be replaced in the above examples with any branch name

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 18
    And can even omit the local path if that is the current directory. – Tony Wall Jun 21 '14 at 19:26
  • 25
    For those extremely noob like me, here is an example: `git diff master:README.md -- README.md` – fabriciorissetto Oct 20 '15 at 13:18
  • 33
    Actually, here is a better example: `git diff origin/master -- README.md` – JDiMatteo Apr 07 '16 at 22:40
  • @JDiMatteo I've been scrolling through page after page looking for that simple command. thanks – Azor Ahai -him- Jun 28 '17 at 20:38
  • I don't know if this is related to git versions nor if this is just another, among the numbers, available command, however this is also diffs a local file against a remote file: `git diff HEAD file_name` – mchar Jul 02 '18 at 10:33
  • @mchar that command diffs the file against the most recent commit in your local repo. If someone else pushed to the remote, you will not see those changes – Code-Apprentice Jul 02 '18 at 13:35
  • @Code-Apprentice got confused by `git fetch **origin**` as I figured that was some special origin fetch. But I realise you meant to use markdown bold like this: git fetch **origin** – andrvibo Apr 13 '22 at 07:57
  • @andrvibo Yes, that is correct. The asterisks are for emphasis, but code formatting doesn't allow bold. The current version of this answer is corrected for a mistake that rob commented on. – Code-Apprentice Apr 13 '22 at 14:41
131

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.

janos
  • 120,954
  • 29
  • 226
  • 236
39

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

git diff @{upstream}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ratna Halder
  • 1,954
  • 16
  • 17
2

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 necessarily its master branch - any branch) to FETCH_HEAD, 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 the same file before I follow the Git stages untrackedstagedcommit, which help me to avoid unnecessary resolve merge conflict with a remote team or make a new local branch and compare and merge on the main branch.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
adg
  • 99
  • 6
2

for me just using the filename without branches worked:

git diff filename
Damien Mattei
  • 358
  • 4
  • 9
1

I tried a couple of solutions, but I think an easy way is 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

After running this script, you will be finished comparing the local Git and remote Git using the last commit number.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ati ince
  • 111
  • 1
  • 6
1

To check with the 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • 1
    "staging" as in Git staging? Or literally "staging"? Or are they the same thing? Can you elaborate? Please respond by [editing your answer](https://stackoverflow.com/posts/63606778/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jun 13 '21 at 15:44
0

A 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 the GitHub help page here and the previous Code-Apprentice answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ng10
  • 1,660
  • 1
  • 15
  • 19