Skip to content
LatchkeyLatchkey home

git revert: Usage, Options & Common CI Errors

git revert is the safe undo: it records a new commit that reverses an earlier one.

Unlike reset, revert never rewrites history, so it is safe on shared and protected branches.

What it does

git revert applies the inverse of the changes from a commit and records the result as a new commit, leaving the original history untouched.

Common usage

Terminal
git revert <sha>
git revert --no-commit <sha>      # stage the revert, commit later
git revert -m 1 <merge-sha>       # revert a merge, keep mainline 1
git revert --abort

Options

FlagWhat it does
-m <parent>Specify the mainline parent when reverting a merge
--no-commit / -nStage the revert without committing
--continueResume after resolving a conflict
--abortCancel an in-progress revert

Common errors in CI

error: commit <sha> is a merge but no -m option was given - reverting a merge needs a mainline parent. Use git revert -m 1 <merge-sha>. Conflicts during a revert pause it; resolve, git add, then git revert --continue.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git revert: Usage, Options & Common CI Errors?
Unlike reset, revert never rewrites history, so it is safe on shared and protected branches.
What it does?
git revert applies the inverse of the changes from a commit and records the result as a new commit, leaving the original history untouched.
Common errors in CI?
error: commit <sha> is a merge but no -m option was given - reverting a merge needs a mainline parent. Use git revert -m 1 <merge-sha>. Conflicts during a revert pause it; resolve, git add, then git revert --continue.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card