git add only modified changes and ignore untracked files
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A common Git workflow is staging edits to tracked files while leaving new untracked files untouched. This is useful when you created temporary notes or generated files but only want to commit changes to existing code. Git supports this directly with git add -u and a few related patterns, but it helps to understand exactly which file states the command touches.
Understand Tracked vs Untracked
Git treats files in three broad states:
- Tracked and modified.
- Tracked and deleted.
- Untracked and new.
git add -u stages modifications and deletions for tracked files only. It ignores untracked files by design.
Stage Only Modified Tracked Files
Basic command:
Equivalent long form:
After running, verify what is staged:
This gives a clean commit when you do not want accidental inclusion of new files.
Restrict Update Staging to a Path
You can limit update staging to specific directories.
This is helpful in monorepos or large projects where you only want staged updates in one area.
Compare with Similar Commands
Developers often confuse related git add variants:
git add .stages modified tracked plus untracked files in current path.git add -Astages everything including deletions and untracked files.git add -ustages tracked file updates and deletions only.
Use command intent, not habit. Wrong staging commands are a common source of noisy commits.
Another related shortcut is git commit -am "message", which also skips untracked files. It can be convenient, but many teams prefer git add -u first because it gives you a staged diff review step before the commit is created.
Interactive Control with Patch Mode
If you want only part of modified tracked files, combine update behavior with patch mode.
Patch mode lets you stage selected hunks while still avoiding bulk accidental staging. This is ideal for splitting refactor and bug fix changes into separate commits.
Handle Deletions Intentionally
Remember git add -u includes tracked deletions. If that is not desired, restore the deleted file before commit.
Review staged diff before committing:
This catches unintended deletions early.
Use .gitignore for Persistent Noise
git add -u ignores untracked files, but repeated temporary files can still clutter git status. Add long term noise patterns to .gitignore.
This improves signal during review and reduces accidental staging risk when using broader add commands later.
Practical Commit Flow
A reliable routine:
git statusgit add -ugit diff --stagedgit commit -m "..."
This takes seconds and keeps commit history focused.
Helpful Aliases for Consistent Usage
Teams can reduce command mistakes with small aliases:
Then your flow becomes:
These aliases are optional but improve consistency for frequent commit workflows.
If your team uses pre-commit hooks, run hooks before final commit to ensure staged tracked updates still satisfy lint and test expectations. This prevents partial staged changes from breaking branch quality checks.
For newer repositories using git restore and git switch, the staging behavior is unchanged. The modern command set changes recovery and branch movement syntax, not what git add -u selects.
Common Pitfalls
- Using
git add .when intention was tracked only updates. - Forgetting that
git add -ustages deletions as well. - Committing without reviewing staged diff.
- Relying on update staging alone instead of maintaining
.gitignore. - Mixing unrelated changes when patch mode would produce cleaner commits.
Summary
- Use
git add -uto stage only modified or deleted tracked files. - Untracked files are intentionally ignored by this command.
- Verify staging with
git statusandgit diff --staged. - Use path limited update staging for targeted commits.
- Combine with patch mode and
.gitignorefor precise, clean history.

