Sub topic 7
Commit a Focused Change
You have a feature branch and a file to edit. This chapter covers how to turn that edit into a commit a maintainer can review quickly.
This chapter assumes you can run git add, git commit, git status, and git log. It also assumes you have already created a feature branch, as in the previous chapter.
What "focused" means
A focused commit does one thing. A focused pull request usually holds one commit or a few related ones. Reviewers read your change line by line, so small and single-purpose changes get reviewed faster and are easier to accept.
Good signs of a focused change:
- You can describe it in one short sentence without using "and".
- Every changed line serves that one purpose.
- It does not mix a bug fix with reformatting or renaming.
Maintainers often reject or ask you to split pull requests that mix unrelated work. Splitting later is harder than staying focused from the start.
Make and review the change
Edit the file, then check what Git sees before you stage anything.
git status
git diffgit status lists the changed files. git diff shows the exact lines changed. Read the diff as if you were the reviewer. Look for stray edits, such as trailing whitespace, a debug line, or an editor that reformatted the whole file.
Stage only what belongs
Stage files by name instead of using git add .. This keeps unrelated files out of the commit.
git add README.md
git statusIf one file holds two unrelated edits, stage it in pieces:
git add -p README.mdGit shows each changed block (a "hunk") and asks whether to stage it. Answer y to stage it, n to skip it, or s to split a large hunk into smaller ones.
Write a clear commit message
The usual shape is a short summary line, a blank line, then an optional body.
git commit -m "Fix typo in README installation section"For a change that needs explanation, run git commit without -m. Your editor opens so you can write a body:
Fix typo in README installation section
The install command said "pip instal". Users who copied it
got "No such command" and had to guess the correct spelling.Guidelines for the summary line:
- Keep it to about 50 characters where you can.
- Use the imperative mood: "Fix typo", not "Fixed typo" or "Fixes typo".
- Say what changed. "Update file" tells the reviewer nothing.
- Follow the project's convention if it has one. Some projects require prefixes such as
docs:orfix:. CheckCONTRIBUTING.mdandgit log --onelineto see the existing style.
Use the body to explain why the change is needed, since the diff already shows what changed.
Check the result
git log --oneline -3
git show --statgit show --stat lists the files in your latest commit. If it includes a file you did not intend, fix it before pushing. To fix the most recent commit that you have not pushed, stage the correction and run:
git commit --amendCommon errors
| Problem | Cause | Fix |
|---|---|---|
| Commit includes unrelated files | Used `git add .` | Run `git reset HEAD~1 --soft`, unstage the extra files with `git restore --staged FILE`, and commit again |
| "Please tell me who you are" | Name and email not configured | Run `git config --global user.name` and `git config --global user.email` |
| Whole file shows as changed | Line endings or editor formatting changed | Restore the file and redo the edit with a plain change |
Exercise
You fixed a typo in README.md and also noticed a broken link in the same file. How do you keep the commits focused, and what command lets you stage them separately?
Show answer
Make two commits, one per change. Run git add -p README.md and stage only the typo hunk, then commit it with a message such as "Fix typo in README installation section". Next stage the link hunk and commit it with its own message, such as "Fix broken link in README". If the project prefers it, send the link fix as a separate pull request.