Challenge ·Easy

Git Staging: Split Tangled Changes Into Clean Commits

A small fix tangles two unrelated changes into one file, and a second file gets left out entirely. Split the mess into clean, separate commits.

Second day, and Niki already feels less like a guest. The folder even has a normal name now, beacon-api. Small fix today, nothing dramatic, a timeout value that support keeps asking about.

She makes the change, and while she is already in the file, fixes a typo in the error message sitting two lines below it. Two small things, one file, no reason to think twice about it.

Before she runs git commit, she hears Teo's voice from yesterday in her head: explain what this commit does. She cannot, not honestly, not in one sentence. So she checks first, out of habit, git status before anything else.

Two files show up, not one. config/staging.yaml has a change she made and forgot about, still sitting there uncommitted from earlier. And auth/token.go shows one block of changes with the timeout fix and the typo fix tangled together, impossible to describe honestly as one thing.

The File She Almost Forgot

config/staging.yaml is not part of today's fix. It is leftover from something else, still uncommitted, easy to miss if she only looks at the file she meant to change. It gets a commit of its own, on its own, before she touches anything else.

Two Fixes, One File

auth/token.go has the timeout fix and the typo fix sitting together, close enough in the file that git shows them as one single block of changes. The timeout fix goes into its own commit first, message: api: fix connection timeout. Nothing about the typo belongs in it.

Teo's tip

git diff --staged shows exactly what a commit would contain right now, before you commit it. git add -p auth/token.go walks through the file's changes piece by piece. If a piece still has both fixes mixed together, press s first to split it, then y to stage one half or n to skip it. git add -e auth/token.go is the manual fallback, editing the patch text directly before staging, if splitting is not enough.

The Typo, On Its Own

The typo fix goes into a second commit, message: auth: correct error message wording. Nothing about the timeout belongs in this one. Once both fixes are committed separately and the forgotten file has its own commit too, nothing should be left uncommitted anywhere.

Teo never even has to ask this time. He just glances over once, sees three commits instead of one, and goes back to whatever he was doing. Small thing. Still the good kind of small.