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.
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.
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:
Use git clean to Remove Untracked Files
If you really want to delete untracked files, use git clean.
Preview first:
Remove untracked files and directories:
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:
Remove untracked and ignored files:
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:
- preview what will be removed
- reset tracked files if needed
- clean untracked files only when you are sure
For example:
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 --hardto delete untracked files automatically. - Running
git clean -fdwithout previewing with-nfirst. - Forgetting that ignored files require
git clean -fdxrather than just-fd. - Treating
resetandcleanas interchangeable commands. - Deleting local scratch files unintentionally because the working tree categories were not understood.
Summary
- '
git reset --hard HEADresets tracked files, not untracked files.' - Untracked files remain because they are outside the index and commit state.
- Use
git clean -fdto remove untracked files and directories. - Use
git clean -fdxonly when you also want to remove ignored files. - Preview
git cleanwith-nbefore running the destructive version.
Related reading
- Git reset single file in feature branch to be the same as in master/main
- git returns http error 407 from proxy after CONNECT
- git revert back to certain commit
- Git, rewrite previous commit usernames and emails
- Git says a file is unmerged and I can''t commit, but the file seems to be merged
- Git says remote ref does not exist when I delete remote branch
- Git See my last commit
- Git Server Like GitHub?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.