Arezgitfield notes / engineering
Git workflowsUPDATED OCT 9, 2026

Stage Focused Commits with Git Add Patch

Separate mixed edits into independently testable commits using partial staging, staged diff inspection, and verification from a clean checkout.

Arezgit Editorial5 min read
AREZGIT / FIELD NOTEGIT WORKFLOWS
A passing test in your working directory does not prove that the commit you are about to create will pass. The directory may contain an unstaged helper, an unrelated configuration fix, or a
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A passing test in your working directory does not prove that the commit you are about to create will pass. The directory may contain an unstaged helper, an unrelated configuration fix, or a new file that the index does not include. Partial staging helps separate mixed work, but the result needs verification as its own runnable snapshot.

This guide covers preparing focused commits from tracked text changes and checking their independence. The examples use synthetic paths and Git commands available in Git 2.45.2; they work in PowerShell or Bash from the repository root.

Treat the index as a separate version of the project

The Git add manual documents that staging records file content at the time of the command. Later edits remain outside that staged snapshot until you stage them again. Patch mode lets you choose individual hunks between the index and working tree.

Think of an edited file as potentially having three relevant versions: the committed file, the indexed file, and the file on disk. A formatter or editor can change the third without changing the second. A staged file is therefore not a promise that all of its current contents will enter the commit.

Before selecting changes, inspect the two differences separately:

git status --short --branch
git diff -- src/payment.ts
git diff --cached -- src/payment.ts

The Git diff documentation distinguishes the working-tree comparison from the cached comparison with the current commit. If both produce output, explain which edits belong to each side before continuing. This catches the common mistake of reviewing the latest on-disk file while committing an older staged version.

Choose a behavioral unit before choosing hunks

In a synthetic example, one file contains a timeout correction, a variable rename, and a change to an error message. Those edits are close together, but proximity is not a useful commit boundary. Ask which edits establish one observable behavior and which evidence will verify that behavior.

The timeout correction may need a matching test and a default configuration change. Include those supporting changes even if they live in different directories. Keeping the commit small should not make it incomplete. Conversely, exclude the rename when it contributes nothing to understanding or testing the fix.

Write the intended commit message before staging. A sentence that needs several unrelated clauses often reveals multiple units of work. Use that sentence as a selection rule while reading the patch, then revisit it if a necessary dependency changes the scope.

Avoid splitting a migration, generated schema, and its consumer merely to achieve a smaller line count. The review unit should be the smallest change that remains meaningful and usable on its own. For broader review criteria, use the guide to reviewing a Git diff.

Select hunks and inspect the assembled result

Start patch mode with a narrow path selection:

git add -p -- src/payment.ts tests/payment.test.ts

Use the interactive prompt's help when unsure about a choice. Accept only the relevant hunks, reject unrelated ones, and split a hunk when Git offers that option. If two edits cannot be separated clearly, reorganize the working edits or include the complete dependency rather than manually manufacturing a confusing patch.

For a new text file, ordinary patch mode needs an index entry. The documented intent-to-add operation provides one without staging the full contents:

git add -N -- tests/payment.test.ts
git add -p -- tests/payment.test.ts

Then inspect the complete staged change, including every supporting file:

git diff --cached --stat
git diff --cached --check
git diff --cached

The whitespace check is a limited mechanical check; it does not establish correctness. Read the final patch as if it came from another contributor. Look for references to helpers that are still unstaged, tests asserting a behavior that is absent, and generated files whose source has not been included.

Verify the commit from an independent checkout

Once the staged patch matches the intended change, run the usual checks and create the local commit. Preserve the remaining working edits in place. For the synthetic example:

git commit -m "Handle payment timeout before retrying"
git worktree add --detach ../payment-commit-check HEAD

Choose an unused sibling directory and inspect the new checkout before installing dependencies or running scripts. Follow the project's lockfile-preserving installation procedure and provide disposable configuration. A worktree contains tracked files from the commit, so it exposes dependencies that were available only in the original working directory.

Run the focused regression test and the required project checks there. A missing import or absent fixture is actionable evidence that the commit boundary is incomplete. Add the supporting change through a normal follow-up commit or revise an unshared local commit according to your team's policy. Do not rewrite history that other contributors already use without coordination.

The worktree guide explains runtime isolation and cleanup. Dependencies, ports, and local databases still need separate ownership; a second checkout alone does not isolate external resources.

Recheck after tools modify files

Hooks and formatters can change the working directory during the commit workflow. After committing, inspect the resulting commit and remaining edits. Confirm that the commit contains the tested behavior and that leftover changes still have a clear purpose.

Binary files and generated bundles rarely offer useful hunk boundaries. Prefer staging them alongside their source change as a verified unit. If generation introduces unrelated churn, fix the generation process rather than treating a large unreadable patch as an acceptable side effect.

Before removing the verification worktree, confirm it has no unique edits and remove it through Git. Keep the original unfinished work until its own commit is reviewed. The useful result is a history in which each commit explains one behavior and can reproduce its own evidence without borrowing files from the next change.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit