DEV Community

Branchline
Branchline

Posted on Fully Autonomous

How to commit only part of a file after Claude Code or Codex edits it

A coding agent can work on a file that already has edits from you. If both sets of edits are in the same file, selecting the file for a commit can take both. Git’s index gives you a place to check exactly what you’re about to commit.

First, confirm you’re in the right folder

Run these commands in the terminal that the agent used:

git rev-parse --show-toplevel
git status --short
Enter fullscreen mode Exit fullscreen mode

The first command prints the root of the current worktree. This matters when an agent is working in a separate worktree. The second command lists changes in that worktree, including untracked files.

Stage a hunk and check both sides of the index

Suppose src/search.ts has one fix you want to commit and a separate experiment that needs more work. Start by reading the unstaged change, then stage the fix interactively:

git diff -- src/search.ts
git add -p -- src/search.ts
git diff --cached -- src/search.ts
git diff -- src/search.ts
Enter fullscreen mode Exit fullscreen mode

In git add -p, use y for a hunk you want to stage and n for one you want to leave in the worktree. If Git puts the fix and experiment in the same hunk, try s to split it where Git offers that option. When they can’t be split cleanly, it’s usually easier to untangle the edits in your editor and review the result.

git diff --cached shows the staged version relative to the last commit. git diff shows what’s still unstaged. Before committing, run git diff --cached once without a filename so you can also catch staged changes in other files.

When you’re happy with the staged patch, git commit records it. Skip git commit -a in this workflow because it also stages modifications and deletions in tracked files.

One more check before pushing

If the branch already tracks an upstream, git log --oneline @{upstream}..HEAD shows which local commits aren’t in that upstream ref. Use git fetch first if you need current remote-tracking refs. An earlier local commit can go out alongside the one you just made.

This guide is from the Branchline project. Branchline is a free, MIT-licensed Mac Git client. In the Commit panel, Git Index mode lets you commit the changes you staged in the terminal. The app’s built-in demo gives you a sample repository and a local remote to practice on.

Branchline Git review guide and demo

This article was prepared with AI assistance. The partial-staging workflow was checked on a disposable Git repository.

Top comments (0)