git
git diff
untracked files
version control
software development

Can I use git diff on untracked files?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Not directly in the ordinary tracked-file sense. git diff compares Git states such as the working tree, the index, and commits, and an untracked file is not yet in any of those states, so Git has nothing in its history or index to compare it against.

Why git diff Skips Untracked Files

An untracked file is simply a file Git sees in your working directory that has never been added to the index. Since git diff normally answers questions like:

  • what changed since the last commit
  • what changed between the index and the working tree
  • what changed between two commits

an untracked file has no left-hand side for Git to diff against.

That is why a normal git diff does not show its contents.

Two Practical Ways to Diff an Untracked File

If what you really want is "show me this file as if it were new," there are two common solutions.

1. Use git diff --no-index

This tells Git to act as a general-purpose diff tool instead of a repository-state diff tool.

bash
git diff --no-index -- /dev/null path/to/newfile.txt

On Windows, you can compare against NUL instead of /dev/null in shells that support it, or use another empty file as the baseline.

This produces a diff that shows the whole untracked file as newly added content.

2. Use git add -N

git add -N adds an "intent to add" entry to the index without staging the file contents fully. After that, git diff can show the working tree content against the empty indexed placeholder.

bash
git add -N path/to/newfile.txt
git diff -- path/to/newfile.txt

This is a very handy trick because it keeps you inside normal Git repository semantics.

If you later want to remove the intent-to-add entry:

bash
git reset path/to/newfile.txt

Choose the Right Method

Use --no-index when:

  • you want a one-off textual diff
  • the file is not necessarily part of the repository yet
  • you want Git as a standalone diff tool

Use git add -N when:

  • the file really is headed toward version control
  • you want to inspect it with normal repository-aware Git commands
  • you may stage it soon anyway

Both approaches are useful, but they answer slightly different workflows.

There is also a practical difference in tooling. If your editor or IDE already integrates with normal Git diff commands, the add -N trick often fits better than a one-off --no-index invocation.

Common Pitfalls

  • Expecting plain git diff to show untracked file content automatically.
  • Confusing git add -N with fully staging the file contents.
  • Forgetting that git diff --no-index is working more like a generic diff command than a repository-state comparison.
  • Leaving intent-to-add entries around and later wondering why the file no longer appears purely untracked in status output.
  • Treating "untracked" as a diff format problem when it is really a missing-baseline problem in Git's object model.

Summary

  • Plain git diff does not operate on untracked files because Git has no tracked baseline for them.
  • 'git diff --no-index can compare an untracked file against an empty file or another file.'
  • 'git add -N creates an index placeholder so git diff can show the new file as unstaged content.'
  • Use --no-index for one-off comparisons and add -N for repository-oriented workflows.
  • The key limitation is not diffing itself, but the absence of a tracked comparison state.

Course illustration
Course illustration

All Rights Reserved.