Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff between two different files

Tags:

git

diff

In HEAD (the latest commit), I have a file named foo. In my current working tree, I renamed it to bar, and also edited it.

I want to git diff foo in HEAD, and bar in my current working tree.

like image 349
MiJyn Avatar asked May 22 '13 03:05

MiJyn


People also ask

Can I use git to compare two files?

You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch. Let's compare two commits in our Git repository. The above command will perform a diff operation across our two commits.


2 Answers

Specify the paths explicitly:

git diff HEAD:full/path/to/foo full/path/to/bar

Check out the --find-renames option in the git-diff docs.

Credit: twaggs.

like image 101
Steven Wexler Avatar answered Oct 21 '22 12:10

Steven Wexler


I believe using --no-index is what you're looking for:

git diff [<options>] --no-index [--] <path> <path> 

as mentioned in the git manual:

This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.

like image 35
mh sattarian Avatar answered Oct 21 '22 11:10

mh sattarian