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
Flag
What it does
-m <parent>
Specify the mainline parent when reverting a merge
--no-commit / -n
Stage the revert without committing
--continue
Resume after resolving a conflict
--abort
Cancel 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@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit 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.