Git
git reset
untracked files
version control
development tips

git reset --hard HEAD leaves untracked files behind

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

git reset --hard HEAD resets tracked files and the index to match HEAD, but it does not remove untracked files. That behavior is intentional. Untracked files are outside the index, so reset leaves them alone. If you also want to delete untracked files, the command you are looking for is usually git clean.

What git reset --hard Actually Resets

git reset --hard HEAD affects two things:

  • the index
  • the working tree copies of tracked files

It makes tracked files match the chosen commit exactly.

bash
git reset --hard HEAD

This discards modifications to tracked files and unstages tracked changes, but it does not touch files Git is not tracking.

That is why a new file you created but never added with git add remains after the reset.

Why Untracked Files Remain

Git treats untracked files as files it does not yet manage. Since they are not part of the index or the commit you reset to, git reset has no reason to modify them.

That behavior is also a safety feature. It helps avoid accidentally deleting new work that Git has not been told to track yet.

So this is expected:

bash
git reset --hard HEAD
# tracked edits are gone
# untracked files are still present

Use git clean to Remove Untracked Files

If you really want to delete untracked files, use git clean.

Preview first:

bash
git clean -nd

Remove untracked files and directories:

bash
git clean -fd

This is the usual companion to reset --hard when the goal is “make my working directory match the repository and remove extra local junk.”

Ignored Files Are Separate Again

Ignored files are another category. If you want to remove ignored files too, use -x.

Preview:

bash
git clean -ndx

Remove untracked and ignored files:

bash
git clean -fdx

This is more destructive, because it can remove generated files, local environment files, and other ignored artifacts.

A Safe Cleanup Sequence

A good discipline is:

  1. preview what will be removed
  2. reset tracked files if needed
  3. clean untracked files only when you are sure

For example:

bash
1git status
2git clean -nd
3git reset --hard HEAD
4git clean -fd

That sequence is much safer than running destructive commands without checking the impact first.

When reset --hard Is Enough

Do not automatically pair reset --hard with clean. Sometimes leaving untracked files alone is exactly what you want. For example, you may have local notes, scratch files, or generated output you are not ready to delete.

Use clean only when the intent is explicit.

Think in Git Categories

The confusion usually disappears once you separate three categories:

  • tracked files: known to Git and affected by reset
  • untracked files: unknown to Git and ignored by reset
  • ignored files: intentionally excluded and removed only with stronger clean options

git reset --hard only targets the first category.

Common Pitfalls

  • Expecting git reset --hard to delete untracked files automatically.
  • Running git clean -fd without previewing with -n first.
  • Forgetting that ignored files require git clean -fdx rather than just -fd.
  • Treating reset and clean as interchangeable commands.
  • Deleting local scratch files unintentionally because the working tree categories were not understood.

Summary

  • 'git reset --hard HEAD resets tracked files, not untracked files.'
  • Untracked files remain because they are outside the index and commit state.
  • Use git clean -fd to remove untracked files and directories.
  • Use git clean -fdx only when you also want to remove ignored files.
  • Preview git clean with -n before running the destructive version.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.