Sub topic 11
Writing revert commits
This chapter assumes you know basic Git (add, commit, log, branches), the Conventional Commits structure of type, scope, description, body, and footers, and the MAJOR.MINOR.PATCH idea from Semantic Versioning. It covers how to record a revert so that history, changelogs, and release tools can still understand it.
What the specification says
The Conventional Commits 1.0.0 specification does not define revert handling itself. It only suggests one approach: use a revert type, and add a footer that lists the hashes of the commits being reverted. Tools and teams fill in the rest, so decide on a house style and write it down.
The example from the specification looks like this:
revert: let us never again speak of the noodle incident
Refs: 676104e, a215868The parts follow the usual anatomy:
revertis the type.- The description says what is being undone, in the same short style as any other commit.
- The
Refs:footer is intoken: valueform and names the original commits by their abbreviated hashes.
Creating a revert with Git
git revert does not rewrite history. It adds a new commit that applies the inverse of an earlier one. This makes it safe on shared branches, unlike git reset, which moves a branch backward and forces others to repair their copies.
git revert 676104eGit opens your editor with a default message like this:
Revert "feat(parser): add support for array literals"
This reverts commit 676104e8c1f0b2d3a4e5f60718293a4b5c6d7e8f.This default does not follow the convention. The subject starts with Revert, not revert:, so a commit-msg hook or commitlint will reject it. Rewrite the message before you save:
revert: remove array literal support in the parser
The ArrayNode change broke parsing of nested expressions.
Refs: 676104eIf you want to edit the message in a separate step, run git revert --no-edit 676104e and then git commit --amend. Alternatively, use git revert -n 676104e to stage the inverse changes without committing, then run git commit with your own message.
Reverting several commits
You can pass more than one hash to git revert. To produce a single commit, use -n so Git stages all the inverses, then commit once and list every hash in the footer:
git revert -n a215868 676104e
git commit -m "revert: undo the noodle incident" -m "Refs: 676104e, a215868"Each -m adds a paragraph, so the second one becomes the footer after a blank line.
How reverts affect versions and changelogs
A revert is a normal commit for the purposes of Semantic Versioning. The specification does not assign revert a bump, so the result depends on the tool.
| Situation | Typical result |
|---|---|
| Revert of a commit that was never released | Often best to drop both from the release notes by hand, since users never saw either change |
| Revert of a released `feat` | Tool-dependent. Many tools list it under a reverts section, and some bump PATCH |
| Revert of a released breaking change | The revert itself removes the break, but the earlier MAJOR release already happened |
Check how your release tool treats the revert type before you rely on it. Some tools need explicit configuration to include it.
Choosing between revert and fix
Use revert when the goal is to undo a specific earlier commit as it was. Use fix when you are correcting behavior with new code, even if some of the original lines disappear. If you revert only part of a commit, or combine the revert with other changes, a fix or refactor with a body that explains the history is often clearer.
Finding reverts later
Because the type is a consistent prefix, plain git log can list them:
git log --oneline --grep='^revert'To see what a given revert undid, read its footer and run git show 676104e.
Exercise
You run git revert 676104e and Git proposes the subject Revert "feat(parser): add support for array literals". Your repository uses a commit-msg hook that requires conventional types. Why would the commit fail, and what message would you write instead?
Show answer
The hook's pattern expects a lowercase type followed by a colon, such as revert:. Git's default subject starts with a capitalized Revert and a quoted title, so it does not match. Rewrite it, for example: revert: remove array literal support in the parser, then a blank line and a footer Refs: 676104e.
Exercise
Why is git revert preferred to git reset plus a force push on a branch that teammates have already pulled?
Show answer
git revert adds a new commit and leaves existing history intact, so everyone can pull it normally. reset with a force push rewrites shared history and forces teammates to repair their local branches.