Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two files not in repo using git

Tags:

git

compare

diff

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.

How do I compare 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. Order does matter when you're comparing branches.

How can I find the difference between two files?

Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.


git's diff is more functional than the standard unix diff. I often want to do this and since this question ranks highly on google, I want this answer to show up.

This question: How to use git diff --color-words outside a Git repository?

Shows how to use git to diff files where at least one of them is not in the repository by using --no-index:

git diff --no-index file1.txt file2.txt

It doesn't matter which one is tracked by git and which is not - one or both can be untracked, eg:

$ date > x
$ sleep 2
$ date > y
$ git diff --color-words --no-index x y
diff --git a/x b/y
index 6b10c7c..70f036c 100644
--- a/x
+++ a/y
@@ -1 + 1 @@
Wed Jun 10 10:57:45|10:57:47 EDT 2013

The color can't be shown here so I separated the changes with a pipe in the example.


diff -u

will give similar unified context output to git's diff.


Just use the diff command:

diff file1.ext file2.ext