git
version control
git commit
programming
gitignore

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:

bash
git add -A

Then unstage the file you do not want in the commit:

bash
git restore --staged path/to/file

On older Git versions, the equivalent is:

bash
git reset HEAD path/to/file

Now commit as usual:

bash
git commit -m "Commit everything except one file"

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:

bash
git status
git diff --staged

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:

bash
git add --all -- ':!path/to/file'

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:

bash
git add -p

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:

bash
git status
git diff --staged --stat

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 .gitignore to 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 with git 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 -A followed by git 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 .gitignore only for files that should generally remain untracked.
  • Use git add -p when you need partial-file control instead of whole-file exclusion.

Course illustration
Course illustration

All Rights Reserved.