Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff current file version and previous one remote repository

Tags:

git

diff

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.

like image 231
OscarRyz Avatar asked May 16 '12 22:05

OscarRyz


People also ask

How do I compare two versions of files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

How do I get the diff of a file in git?

By executing the git diff command, we can see the differences between these two files. By default, the git diff command produces a diff for all files between the latest commit and the current state of the repository.

How can you tell the difference between a local and remote branch?

To compare the local and remote branches in Git, first, open up the Git terminal and execute the “$ git fetch” command to fetch and update the remote branches. Then, run the “$ git branch -a” command to display all remote and local branches.

How can I see the diff of a commit?

To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..


3 Answers

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
like image 179
meagar Avatar answered Nov 04 '22 10:11

meagar


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).

like image 28
GoZoner Avatar answered Nov 04 '22 09:11

GoZoner


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
like image 3
dinosaur Avatar answered Nov 04 '22 08:11

dinosaur