Add all files to a commit except a single file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want to commit everything except one file, the simplest workflow is usually to stage everything and then unstage the file you want to leave out. That approach is easy to reason about and works on essentially every modern Git installation.
Stage Everything, Then Remove One File from the Index
Start by staging all current changes:
Then unstage the file you do not want in the commit:
On older Git versions, the equivalent is:
Now commit as usual:
The file stays modified in your working tree, but it is no longer part of the staged snapshot.
Why This Workflow Is Safe
Git commits the index, not your entire working directory automatically. That means you can shape the commit by editing what is staged without losing the local file changes you left out.
A good inspection sequence is:
After unstaging the excluded file, git status should show it under "Changes not staged for commit" while the rest of your changes remain staged.
Use Pathspec Exclusion When You Need It
Git also supports exclusion-style pathspecs in some commands:
This can be convenient, but it is easier to mistype and less obvious to casual Git users. For one file, the stage-then-unstage workflow is usually clearer and more portable across different team habits.
Know When .gitignore Is the Wrong Tool
.gitignore is for files that should usually or always stay untracked. It is not the right tool when the file is already tracked or when you only want to skip it for one commit.
If the file is tracked and you change it, .gitignore will not stop Git from noticing those modifications. In that situation, selective staging is the right approach.
Handle Partial Commits Deliberately
If you need even finer control, use interactive staging:
That lets you include some hunks from a file and leave others for later. It is a better fit when the problem is not "exclude this whole file" but "exclude part of this file."
Verify the Final Snapshot Before Committing
A quick last check prevents accidental inclusion:
That makes it obvious whether the excluded file is still staged or whether the commit contains only the intended set of changes.
Common Pitfalls
- Using
.gitignoreto try to exclude a tracked file from one commit. Ignore rules do not solve that case. - Forgetting to inspect the staged snapshot after
git add -A. Always confirm withgit diff --staged. - Mistyping the file path when unstaging, which leaves the file in the commit accidentally.
- Assuming unstaging discards local edits. It only removes the file from the index; the working tree change remains.
- Reaching for complex pathspec exclusions when a simple stage-then-unstage flow would be clearer.
Summary
- The simplest method is
git add -Afollowed bygit restore --staged <file>. - Older Git versions can use
git reset HEAD <file>instead. - A commit is built from the staged index, not from every modified file automatically.
- Use
.gitignoreonly for files that should generally remain untracked. - Use
git add -pwhen you need partial-file control instead of whole-file exclusion.

