What should you do before committing? Here’s a simple method for clean commits your teammates will understand right away.
1. List the changed files
git status
Check that no file was changed by mistake. If a file has nothing to do with this commit, don’t add it.
2. Review the added code
Go over each change (with git diff, covered in chapter 11) and look for:
- leftover debugging: a
dump(), adie(), aconsole.log()…; - useless spaces or line breaks, added by mistake or by your editor.
Remove them and save: your commit should only contain real changes.
3. One commit = one topic
Sometimes, the same file holds two unrelated changes: one for this commit, another for a later one. Rather than temporarily deleting code and putting it back afterwards, Git has an option built for this:
git add -p
Git shows you each block of changes (a “hunk”) and asks whether to stage it:
| Key | Action |
|---|---|
y | Yes, stage this block |
n | No, keep it for later |
s | Split the block into smaller pieces |
q | Quit |
Then run git status again: the same file shows up both in the files ready to commit and in the modified files. That’s expected: one part goes into this commit, the other waits for the next one.
4. Write a good message
git commit -m "Add the sign-up page"
- Working with tickets (Jira, GitLab…)? The ticket number is often enough, since the ticket holds all the details.
- Otherwise, write a short, descriptive message, in a few words. Whoever reviews your pull request should understand right away what it’s about.
-msaves you from opening a text editor to write the message: handy for a short message.
The result
A perfect commit is:
- clean code, with nothing forgotten and nothing weird added;
- one topic per commit;
- a clear message.
Exercise · Fix the last commit
You just committed, but forgot to add style.css. The commit isn’t pushed yet. Add the file to that same commit, without creating a new one.
Show the solution
git add style.css
git commit --amend --no-edit
--no-edit keeps the current message. Only use --amend on a commit that’s not pushed yet: it replaces the commit with a new one, which would cause trouble for anyone who already fetched the old one.
What’s Next
In the next chapter, we zoom out: how to organize a project’s branches with branching strategies.
This is Chapter 6 of the Git in Practice course. Follow along to learn how to use Git every day and as a team.