How to commit only modified and not new or deleted files?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes you want a commit that includes only changes to already tracked files, while leaving new files and deletions out of it. Git has shortcuts that are close, but not exact, so the safest approach is to stage only paths whose status is modified. That gives you a narrow, reviewable commit without accidentally bundling unrelated work.
Know Why the Obvious Commands Miss the Mark
A few Git commands seem promising but do not match the requirement exactly:
- '
git commit -astages modified and deleted tracked files' - '
git add -ustages modified and deleted tracked files' - '
git add .stages new files too'
So if your working tree contains modified, deleted, and untracked files at the same time, none of those commands produces a "modified only" commit.
Stage Only M Paths
One practical approach is to ask Git for paths whose change type is M, then stage exactly those files.
The important part is --diff-filter=M, which keeps only modified paths and excludes added and deleted ones. The -z and xargs -0 combination keeps the command safe for file names that contain spaces or unusual characters.
Verify the Index Before Committing
Before creating the commit, inspect what is staged.
Suppose your working tree looks like this:
After staging only modified paths, the cached diff should contain src/app.py but not the deleted or untracked paths.
That quick verification step prevents narrow commits from accidentally becoming mixed commits.
Use Explicit Paths When the Set Is Small
If you already know which modified files should go into the commit, the simplest solution is still to add them by name.
This is often clearer than a pipeline when the file set is small and obvious.
Patch Mode Is Useful for Mixed Files
Sometimes a modified file contains both wanted and unwanted hunks. In that case, path-level staging is still too broad. Use patch mode instead.
Patch mode lets you stage only the portions you want. That is particularly useful when you are splitting a large refactor into smaller logical commits.
Why Narrow Commits Help
A commit that contains only one kind of change is easier to review, easier to revert, and easier to understand later. Excluding untracked files and deletions can be useful when:
- experimental files are not ready yet
- you removed files locally but want to ship code edits first
- you are breaking one messy working tree into several cleaner commits
Git rewards deliberate staging. The more intentional you are about the index, the cleaner the history tends to be.
Common Pitfalls
The biggest pitfall is assuming git commit -a excludes deletions. It does not. It stages deleted tracked files too.
Another common issue is using git add . out of habit and accidentally pulling in new files that were never meant to be committed.
People also often skip the verification step. Running git diff --cached --name-only takes a second and catches staging mistakes cheaply.
Finally, if you use a path pipeline, keep the null-delimited form. Plain line-based piping can break on unusual file names.
Summary
- Git shortcuts such as
commit -aandadd -uinclude deletions, so they are not "modified only." - Use
git diff --name-only --diff-filter=M -z | xargs -0 git add --to stage only modified tracked files. - Verify the staged set before committing.
- Use explicit paths for small file sets and
git add -pfor hunk-level control. - Intentional staging produces cleaner commits and easier code review.

