git
version control
git add
ignore untracked files
code management

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:

bash
git add -u

Equivalent long form:

bash
git add --update

After running, verify what is staged:

bash
git status

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.

bash
git add -u src/

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 -A stages everything including deletions and untracked files.
  • git add -u stages 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.

bash
git add -p

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.

bash
git restore path/to/file

Review staged diff before committing:

bash
git diff --staged

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.

gitignore
*.log
.tmp/
.cache/

This improves signal during review and reduces accidental staging risk when using broader add commands later.

Practical Commit Flow

A reliable routine:

  1. git status
  2. git add -u
  3. git diff --staged
  4. git commit -m "..."

This takes seconds and keeps commit history focused.

Helpful Aliases for Consistent Usage

Teams can reduce command mistakes with small aliases:

bash
git config --global alias.au "add -u"
git config --global alias.st "status --short"
git config --global alias.ds "diff --staged"

Then your flow becomes:

bash
git st
git au
git ds

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 -u stages 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 -u to stage only modified or deleted tracked files.
  • Untracked files are intentionally ignored by this command.
  • Verify staging with git status and git diff --staged.
  • Use path limited update staging for targeted commits.
  • Combine with patch mode and .gitignore for precise, clean history.

Course illustration
Course illustration

All Rights Reserved.