How to Compare Two Files with git diff –no-index
To compare two files that Git does not track, use git diff --no-index old.conf new.conf. The differences appear in the same - and + format as a regular git diff. This is useful when you want to compare old and new config files, or a file you copied and then edited, without using the diff command. Outside a repository, the result is the same with or without --no-index. Inside a repository, however, you cannot omit it when comparing files that are not tracked.
I tested on Ubuntu 24.04, Git 2.43.0, and GNU diffutils 3.10. I set the commit author to an example value (Neko <neko@example.com>). The output shown in this article has no color. I did not test on Windows or macOS, and I did not test an external diff tool (git difftool).
- 1. The Two Files to Compare
- 2. Outside a Repository, the Result Is the Same With or Without –no-index
- 3. Using the Exit Code to Check Whether Files Are the Same in a Shell Script
- 4. Comparing Untracked Files Inside a Repository
- 5. Making the Output Easier to Read with –stat and –word-diff
- 6. Comparing Two Folders
- 7. A Missing File Also Gives Exit Code 1
The Two Files to Compare
I created two config files in a folder that is not a Git repository. new.conf is old.conf with debug changed to true and a retry=3 line added. In this folder, git status returns an error, which shows that Git is not managing these files.
$ printf "port=8080\nhost=example.com\ndebug=false\ntimeout=30\n" > old.conf
$ printf "port=8080\nhost=example.com\ndebug=true\ntimeout=30\nretry=3\n" > new.conf
$ git status
fatal: not a git repository (or any of the parent directories): .git
Outside a Repository, the Result Is the Same With or Without –no-index
When I passed the names of the two files, the differences appeared in git diff format. Adding --no-index did not change the output at all. In both cases, the exit code was 1 when there were differences.
$ git diff old.conf new.conf; echo "終了コード: $?"
diff --git a/old.conf b/new.conf
index 1b22d43..334ddc6 100644
--- a/old.conf
+++ b/new.conf
@@ -1,4 +1,5 @@
port=8080
host=example.com
-debug=false
+debug=true
timeout=30
+retry=3
終了コード: 1
$ git diff --no-index old.conf new.conf; echo "終了コード: $?"
diff --git a/old.conf b/new.conf
index 1b22d43..334ddc6 100644
--- a/old.conf
+++ b/new.conf
@@ -1,4 +1,5 @@
port=8080
host=example.com
-debug=false
+debug=true
timeout=30
+retry=3
終了コード: 1
According to the git diff reference, git diff --no-index <path> <path> compares two paths on the filesystem. You can omit --no-index in two cases: when you run the command outside a folder that Git manages, and when you run it inside such a folder but at least one of the two paths points outside it.
The 1b22d43 and 334ddc6 in the index line are the leading characters of hashes calculated from each file's contents. I calculated each file's hash with git hash-object, and the first seven characters matched.
$ git hash-object old.conf new.conf
1b22d430ef649b49167413074057507cc8406ff0
334ddc6f74d4ed128f284e5b8ccde7e156cec216
Using the Exit Code to Check Whether Files Are the Same in a Shell Script
The official reference says this form behaves the same as using --exit-code. Like the diff command, --exit-code exits with 1 if there are differences and 0 if there are none. When I compared old.conf with same.conf, a copy of it, the command printed nothing and exited with 0.
$ cp old.conf same.conf
$ git diff --no-index old.conf same.conf; echo "終了コード: $?"
終了コード: 0
If you want to know only whether the files are the same, without showing the differences, add --quiet. For old.conf and new.conf, which differ, the command printed nothing and exited with 1.
$ git diff --no-index --quiet old.conf new.conf; echo "終了コード: $?"
終了コード: 1
Comparing Untracked Files Inside a Repository
Inside a repository, the result depends on whether you use --no-index. For this test, I created a repository repo, made one commit, and placed the same old.conf and new.conf in it without running git add.
$ git status --short
?? new.conf
?? old.conf
When I ran git diff old.conf new.conf without --no-index, nothing was printed, and the exit code was 0. Inside a repository, the two names are treated not as "two files to compare" but as paths that narrow down which files to show differences for. According to the official reference, git diff with only paths shows the differences between the working tree and the stage (index). Neither of these files is tracked, so there is nothing to show. If you see exit code 0 and conclude that the files are the same, you will be wrong.
$ git diff old.conf new.conf; echo "終了コード: $?"
終了コード: 0
With --no-index, the output and exit code matched those from running the command outside a repository.
$ git diff --no-index old.conf new.conf; echo "終了コード: $?"
diff --git a/old.conf b/new.conf
index 1b22d43..334ddc6 100644
--- a/old.conf
+++ b/new.conf
@@ -1,4 +1,5 @@
port=8080
host=example.com
-debug=false
+debug=true
timeout=30
+retry=3
終了コード: 1
When I changed one side to a file outside the repository (../old.conf), the command compared two files even without --no-index. The diff header showed the path I passed as is, such as a/../old.conf.
$ git diff ../old.conf new.conf; echo "終了コード: $?"
diff --git a/../old.conf b/new.conf
index 1b22d43..334ddc6 100644
--- a/../old.conf
+++ b/new.conf
@@ -1,4 +1,5 @@
port=8080
host=example.com
-debug=false
+debug=true
timeout=30
+retry=3
終了コード: 1
Making the Output Easier to Read with –stat and –word-diff
The options you normally use with git diff also worked with --no-index. With --stat, the output shows only the file names and the number of added and removed lines.
$ git diff --no-index --stat old.conf new.conf
old.conf => new.conf | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
When only part of a line changes, --word-diff shows the differences word by word, which makes it easier to find what changed. Removed words are wrapped in [-…-], and added words are wrapped in {+…+}. In this config file, there are no spaces around =, so debug=false appeared as a single unit. The official reference also says that, by default, words are separated by whitespace.
For --word-diff-regex, which compares character by character, see the separate article "How to compare working tree, staged, and committed diffs with git diff."
$ git diff --no-index --word-diff old.conf new.conf
diff --git a/old.conf b/new.conf
index 1b22d43..334ddc6 100644
--- a/old.conf
+++ b/new.conf
@@ -1,4 +1,5 @@
port=8080
host=example.com
[-debug=false-]{+debug=true+}
timeout=30
{+retry=3+}
For files that differ only in whitespace, -w (--ignore-all-space) is useful. When I compared old.conf with spaced.conf, which changes port=8080 to port = 8080, the command showed a difference and exited with 1 without -w. With -w, it printed nothing and exited with 0.
$ git diff --no-index old.conf spaced.conf; echo "終了コード: $?"
diff --git a/old.conf b/spaced.conf
index 1b22d43..7c56726 100644
--- a/old.conf
+++ b/spaced.conf
@@ -1,4 +1,4 @@
-port=8080
+port = 8080
host=example.com
debug=false
timeout=30
終了コード: 1
$ git diff --no-index -w old.conf spaced.conf; echo "終了コード: $?"
終了コード: 0
Comparing Two Folders
When you pass folders as the two paths, the command compares the files inside them that have the same names. For this test, v1 contained a.txt and b.txt, and v2 contained an edited a.txt and a new c.txt. With --name-status, the output listed the changed files (M), the files only in v1 (D), and the files only in v2 (A).
$ git diff --no-index --name-status v1 v2
M v1/a.txt
D v1/b.txt
A v2/c.txt
Without options, the command prints the differences for each file in turn. A file that exists on only one side appears as deleted file or new file, and all of its lines appear as removed or added.
$ git diff --no-index v1 v2
diff --git a/v1/a.txt b/v2/a.txt
index 7898192..c1827f0 100644
--- a/v1/a.txt
+++ b/v2/a.txt
@@ -1 +1 @@
-a
+a2
diff --git a/v1/b.txt b/v1/b.txt
deleted file mode 100644
index 6178079..0000000
--- a/v1/b.txt
+++ /dev/null
@@ -1 +0,0 @@
-b
diff --git a/v2/c.txt b/v2/c.txt
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/v2/c.txt
@@ -0,0 +1 @@
+c
A Missing File Also Gives Exit Code 1
If you mistype the name of the file to compare, git diff --no-index prints an error and exits with 1. That is the same value it returns when there are differences. With the same typo, diff -u exits with 2, so the two cases can be told apart. In Git 2.43.0, which I tested, if a shell script treats exit code 1 as "there are differences," a missing file is also counted as a difference.
$ git diff --no-index old.conf nosuch.conf; echo "終了コード: $?"
error: Could not access 'nosuch.conf'
終了コード: 1
$ diff -u old.conf nosuch.conf; echo "終了コード: $?"
diff: nosuch.conf: No such file or directory
終了コード: 2
When I passed only one path, the command printed a usage message starting with usage: and exited with 129.
$ git diff --no-index old.conf > usage.txt 2>&1; echo "終了コード: $?"; head -1 usage.txt
終了コード: 129
usage: git diff --no-index [<options>] <path> <path>



