History
Revert
4 min read
Revert creates a new commit that does the opposite of an earlier commit: what it added goes away, what it removed comes back. The original commit stays in the history, and nothing is rewritten.
It's the right way to undo a commit that has already been pushed and that other people may have
fetched, for example a change that broke main. The push after the revert is a regular push.
| To… | Use | Rewrites history? |
|---|---|---|
| Cancel out a commit that has already been pushed | Revert | No: it adds a commit |
| Remove a commit that hasn't been pushed yet | Drop commit | Yes |
| Remove the branch's last commits | Reset | Yes |
- A commit's menu (right-click in the graph) › Revert commit.
- A branch's menu › Revert commit (applies to the commit at the tip of the branch).
- The item is disabled on merge commits.
- Check out the branch the revert should go into, usually the same as the commit's (for example
main). - Right-click the commit, for example
c3d4e5f Switch the email provider. - Choose Revert commit.
- Versora predicts whether there will be a conflict. Without a conflict, the revert runs right away ("Reverting…"). With a conflict expected, it shows the files first and asks.
- The new commit, with the title
Revert "Switch the email provider", shows up at the top of the current branch.
The revert always goes into the current branch, even if the commit belongs to another branch.
git revert --no-edit c3d4e5f
--no-edit accepts git's default message, without opening an editor: the title
Revert "<commit title>" and the line This reverts commit <full SHA>. in the body. To change the
message afterwards, use Edit commit message before the
push.
If there's a conflict (another commit touched the same lines later), git stops. The changes panel shows the "Revert in progress" bar, with Continue, Skip and Abort (see Operation in progress).
- It finished without a conflict: Ctrl+Z ("Undo: Revert c3d4e5f") removes the revert commit. If it has already been pushed, don't undo it this way: revert the revert.
- It stopped at a conflict: Abort puts everything back. If you resolved it and continued until the end (with Continue, through the commit box or even from the terminal), the whole revert goes into Ctrl+Z when it finishes, with the same name ("Undo: Revert c3d4e5f"), and removes what the revert created. The steps taken during the stop (resolving a file in the conflict editor, for example) leave the undo history at that point.
Conflict preview (when it's on and with git 2.40 or newer):
- The title "The revert will conflict", with "Reverting c3d4e5f ("Switch the email provider") will conflict in 1 file:" and the list of files (up to eight; the rest becomes "• and N more").
- "If you go ahead, the operation stops at the conflict and you resolve it in the conflict editor (Abort puts everything back)."
- Revert anyway goes ahead; Cancel does nothing.
Merge commits: the item is disabled, because reverting a merge requires choosing which side to keep
(git revert -m). Do it from the integrated terminal if you need
to; see the git revert (opens another site) documentation.
- Preferences › Operations › "Cherry-pick and revert" › Predict conflicts before cherry-pick and revert (on by default; needs git 2.40 or newer).
- The company can hide or lock the Operations page through the policy (
ergoOpsinpreferences.hiddenorpreferences.locked); see Company policy.
Cancels out several commits at once, with one revert commit for each, from newest to oldest.
- Select the commits in the graph with Ctrl+click or Shift+click and open the selection menu › Revert 2 commits (the number follows the selection). See Multiple selection.
- Select the commits, for example
c3d4e5f Switch the email providerandd4e5f6a Adjust the email template. - Right-click one of them and choose Revert 2 commits.
- If the preview finds a conflict, it says in which one: "Reverting commit 1 of 2, d4e5f6a ("Adjust the email template") will conflict in 1 file:". Confirm with Revert anyway or cancel.
- At the end: "Reverted 2 commits." The current branch gets two revert commits.
git revert --no-edit d4e5f6a c3d4e5f # from newest to oldest
Going from newest to oldest keeps a revert from running into the lines the following commit changed.
Ctrl+Z ("Undo: Revert 2 commits") removes the revert commits, if the operation finished without stopping. If it stopped, Abort puts everything back.
When the selection doesn't work, the item shows the reason ("Revert 2 commits — reason"):
- "an operation is in progress"
- "the selection includes a stash"
- "the selection includes a merge"