Sub topic 11
Writing revert commits
This chapter assumes you can run git log, git commit, and git revert. It also assumes you know the Conventional Commits structure from earlier chapters: a type, an optional scope, a description, and optional body and footers.
A revert undoes an earlier change. Conventional Commits 1.0.0 has no formal rule for it. The specification says reverts are not defined, and it suggests one approach: use the type revert and add a footer that lists the hashes of the reverted commits. This chapter covers that approach, the problems that come up when Git's default revert message meets your tooling, and how reverts interact with versioning and changelogs.
What a revert is
git revert <commit> creates a new commit that applies the opposite of an earlier commit. It does not delete history. The original commit and the revert both stay in the log. This makes revert safe on shared branches, because nobody has to rewrite commits other people already pulled.
Do not confuse it with two other commands:
git resetmoves a branch pointer and can discard commits. On shared branches it forces everyone to repair their copy.git commit --amendrewrites the most recent commit. It only fits unpublished work.
A revert commit is a real change, so it needs a message that tells readers what happened and why.
The suggested format
The specification's example looks like this:
revert: let us never again speak of the noodle incident
Refs: 676104e, a215868It has three parts:
- The type
revert. It says this commit undoes earlier work, so you can filter for it withgit log --grep='^revert'. - A description in the imperative mood, like any other description. It says what is being undone.
- A
Refs:footer listing the abbreviated hashes of the reverted commits. It uses the standardtoken: valuefooter form.
The footer is the part that makes the revert traceable. A description alone says what was undone, but a reader still has to search for the original commit. With hashes in the footer, they can run git show 676104e right away.
Making the description useful
Good revert descriptions repeat the original subject, so a changelog reader sees what disappeared:
revert: add support for array literalsBetter ones add the reason in the body:
revert: add support for array literals
The ArrayNode change causes the parser to hang on nested arrays deeper
than 64 levels. Reverting until the depth limit is implemented.
Refs: 4f1c9abWithout the reason, someone will try to reintroduce the change in a month and hit the same bug. The body is where you record it.
Scope on a revert
You may keep the scope of the original commit, such as revert(parser): add support for array literals. This helps when you filter by scope or when your linter limits the allowed scopes. Use the original scope if one existed, so the revert groups with the change it undoes.
Git's default message
Git's own message for a revert is different:
Revert "feat(parser): add support for array literals"
This reverts commit 4f1c9ab3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a7.This message has three problems for a conventional history:
- It starts with a capital
Rand has notype:prefix, so it does not match the pattern in a strictcommit-msghook. - A grep such as
--grep='^revert'is case-sensitive by default, and the message does not containrevert:anyway. - The reverted type is hidden inside quotes. Tools that parse the type may read the message as non-conventional and skip it.
Some linters and release tools special-case Git's default format. Others do not. Check what your tools do before you rely on the default.
You have two choices: edit the message before you commit, or configure your tooling to accept Git's format. Editing the message is simpler and works everywhere.
A complete workflow
The following commands revert two commits as one logical change and write a conventional message:
# Find the commits to undo
git log --oneline -8
# Stage the reversal of both commits without committing yet
git revert --no-commit 676104e a215868
# Commit with a conventional message and a Refs footer
git commit \
-m "revert: remove the noodle recipe importer" \
-m "The importer corrupts recipe titles that contain emoji." \
-m "Refs: 676104e, a215868"--no-commit (or -n) applies the reversals to the working tree and the index, and stops. This lets you combine several reverts into one commit. Each -m option adds a new paragraph, so the commit has a description, a body, and a footer separated by blank lines.
If you revert only one commit and want to edit Git's generated message, use git revert <hash> without --no-edit. Your editor opens with the default text. Replace it with a conventional message.
If you already made the revert with Git's message, fix it before pushing:
git commit --amend -m "revert: add support for array literals" -m "Refs: 4f1c9ab"Amend only commits you have not pushed. After a push, leave the message alone or accept the default format.
Reverting a merge commit
A merge commit has two parents, and Git cannot tell which side to keep unless you say so. Pass -m 1 to keep the first parent, which is usually the branch you merged into:
git revert -m 1 <merge-hash>Here -m is a git revert option that selects the parent. It is unrelated to the -m message option of git commit above. Then write the conventional message as before.
With squash merges, each pull request lands as a single normal commit, so you can revert it without -m. This is one reason squash merges pair well with reverts.
Revert or fix forward
A revert is not always the right response to a bug. The choice depends on how well you understand the problem.
Revert when you need the branch healthy quickly and need time to understand the problem. Fix forward when the fix is obvious and small. A revert is quick to make, but it removes the feature, and the people who wanted it will need it back.
Reverts and Semantic Versioning
Chapter 8 mapped feat to MINOR, fix to PATCH, and breaking changes to MAJOR. The specification does not say how revert fits. It is neither feat nor fix, so by default it triggers no version bump in tools that follow the strict mapping.
Think through the cases:
| Situation | Effect on users | Suggested handling |
|---|---|---|
| The reverted commit was never released | None. The change never shipped. | A `revert` commit is enough. No release is needed. |
| A released `fix` is reverted | The bug returns. | Release a PATCH. Some tools do this for `revert` automatically. |
| A released `feat` is reverted | A feature disappears. | This is a breaking change if users relied on it. Consider marking it with `!` or a `BREAKING CHANGE` footer. |
| A released breaking change is reverted | Old behavior returns. | Release a new version. Version numbers cannot go backward. |
Never reuse or retract a version number because of a revert. Versions only move forward, so a revert of a released change is a new release with new contents.
Release tools differ. Some treat revert as a patch-level change by default, some ignore it, and many let you configure it. Decide on a rule for your project and write it down in your contributing guide.
Reverts in changelogs
Changelog generators group commits by type. With revert as its own type, they can show a "Reverts" section, which tells readers what was withdrawn. Without it, the revert may vanish from the output or appear as a noisy Revert "..." entry.
A common problem is a feature that is added, released, reverted, and re-added in the next release. The changelog then shows feat, revert, and feat entries for the same item. To avoid confusion, make the re-landing message say so:
feat(parser): add support for array literals
Re-lands 4f1c9ab with a nesting depth limit of 64.
Refs: 4f1c9ab, 9e8d7c6Reverting a revert
If you revert a revert commit, the original change comes back. Git generates a message such as Revert "Revert "feat: ...", which is hard to read. Skip that path. Instead, create a new commit with the original type, a description of the re-landed change, and a footer that points to both the original commit and the revert, as in the example above. The new commit then documents why the change returned.
Reverts with squash merges and pull requests
With squash merging, the pull request title becomes the commit message. Reverting a pull request on a hosting platform usually creates a new pull request with a title like Revert "feat: add search". Rename that title to revert: add search before you merge, and keep the hash of the squashed commit in the body. The squash commit's hash is the one you want in the Refs: footer, not hashes from the original branch, which no longer exist on the main branch.
Common mistakes
- Leaving Git's default message, which breaks type-based filtering and linting.
- Omitting the
Refs:footer, so nobody can find the original change quickly. - Giving no reason, so the change gets re-added without the problem being solved.
- Using
git reset --hardand a force push on a shared branch instead of a revert. - Reverting a merge commit without
-m 1, which makes Git stop with an error asking for a parent. - Assuming the release tool bumps the version for a revert without checking its configuration.
Exercises
Exercise
You revert two commits, a1b2c3d (adds a CSV export) and e4f5a6b (adds export tests), in one commit. Write the full conventional message, including the reason: the export writes files with the wrong encoding.
Show answer
One correct answer:
revert: remove the CSV export
The export writes files with the wrong encoding and corrupts non-ASCII names.
Refs: a1b2c3d, e4f5a6b
The type is revert, the description is imperative and short, the body gives the reason, and the footer lists both hashes in token: value form. You can create it with git revert --no-commit a1b2c3d e4f5a6b followed by git commit with three -m options.
Exercise
Your project squash-merges pull requests. A merged PR with the title feat(search): add fuzzy matching shipped in version 2.4.0 and breaks a popular workflow. You decide to revert it. Which type do you use, what footer do you add, and what must you remember about the version number?
Show answer
Use the type revert, for example revert(search): add fuzzy matching, and add a Refs: footer with the hash of the squash commit on the main branch. Because the feature already shipped, the revert changes behavior for users, so it needs a new release. The version cannot go back to 2.3.x or reuse 2.4.0. Depending on whether users relied on the feature, release a PATCH or mark the revert as breaking with ! or a BREAKING CHANGE footer, and follow your project's written rule.