OscarRyz
OscarRyz

Reputation: 199215

Diff current file version and previous one remote repository

How do I diff my working file version vs. some previous version in the remote repository?

Say, I pull today, perform 6 - 8 commits to my local copy and then want to see the diff between my latest working version ( of a given file ) and the latest on the remote or any other version.

Upvotes: 16

Views: 19904

Answers (4)

dinosaur
dinosaur

Reputation: 3278

Suppose path/to/file.txt is some file that's committed to the remote branch origin/master and is also in my workspace, committed to the local branch my-branch.

Difference between the latest version of path/to/file.txt committed the remote branch and the (possibly uncommitted) version in my workspace:

git diff origin/master:path/to/file.txt path/to/file.txt

Difference between the version of path/to/file.txt committed the remote branch three commits ago and the (possibly uncommitted) version in my workspace:

git diff origin/master~3:path/to/file.txt path/to/file.txt

Difference between the latest version of path/to/file.txt committed the remote branch and the latest version committed to my-branch:

git diff origin/master:path/to/file.txt my-branch:path/to/file.txt

Upvotes: 3

Amos Folarin
Amos Folarin

Reputation: 2179

git fetch;  #this will attach the remote branch commits to your local tree
git diff FETCH_HEAD    #diff your working dir version of my_file vs the remote tip

same as
git diff remotes/origin/branch 

But you must do a git-fetch first or you won't have the remote's commits locally available to diff against

Upvotes: 0

GoZoner
GoZoner

Reputation: 70155

To see the diff between your 'latest working version' (I'll take that as your working copy) use:

git diff <remote>/<branch>

If you think somebody else has pushed to the remote then you need to fetch the changes:

git fetch <remote> <branch>
git diff <remote>/<branch>

If you want to restrict the diff to just a file or to all files in a directory use:

git diff <remote>/<branch> -- /path/to/file
git diff <remote>/<branch> -- /path/to/           #all files in directory

You can use git difftool ... to start a visual diff tool (assuming one exists on your machine).

Upvotes: 13

user229044
user229044

Reputation: 239311

If you're talking about a remote branch, say, origin/master, you can use ~ and ^ to refer to ancestor commits relative to a branch the same way you can with local branches:

# what change was introduced to origin/master in the last 4 commits?
git diff origin/master origin/master~3

If you want to diff your current working directory against the 5th most recent commit on origin/master, you would omit the first argument:

git diff origin/master~4

Upvotes: 12

Related Questions