How do you undo a commit with git revert?
git revert <commit> creates a new commit that reverses the changes of the commit you name. The original commit stays in the history: nothing is deleted or rewritten. It takes three steps: find the commit, run git revert, check the result.
The examples use orbit-api and its remote origin. The last commit added a faulty search cache, and it is already pushed. Outputs come from Git 2.54, and your IDs will differ.
- Find the commit hash in the compact log:
git log --oneline -3
a28b70f feat: cache search results
d0a7cb2 feat: paginate search results
d7b3ca4 fix: raise search limit to 50
- Run
git reverton it.HEADnames the last commit, and any hash works the same way:
git revert --no-edit HEAD
[main b1cadef] Revert "feat: cache search results"
Date: Sun Nov 8 19:52:23 2026 +0100
1 file changed, 5 deletions(-)
delete mode 100644 src/cache.ts
- Check with
git log --oneline -2:b1cadefnow sits on top ofa28b70f, the commit it cancels.
Without --no-edit, Git opens your editor with that commit message. The git-revert documentation recommends explaining why you revert. Your working tree must be clean first, with no uncommitted changes.
Revert an already pushed commit without a force push
The revert only added a commit, so git status -sb reports [ahead 1] and a plain push goes through:
git push
To ../origin.git
a28b70f..b1cadef main -> main
On GitHub or any other remote, the revert shows up as an ordinary commit. Teammates get it with their next git pull.
The GitLab documentation is explicit: never modify the history of a default or shared branch. Undo remote changes with a new commit instead, which is what git revert does.
A git reset --hard HEAD~1 then git push breaks that rule. Git rejects the push, because your branch is now behind the remote. Only a force push would replace it, and anyone who already pulled the old commit would then hold a different history.
Git revert vs git reset: which one fits your case?
git revert adds a commit and leaves the commit history alone. git reset moves the branch back to a previous commit, so the commits after it leave the branch. With --hard, it also throws away the uncommitted changes in your working directory.
GitLab sums up the trade-off: reset removes the commit, but revert removes only its changes. You can even revert a revert.
If the commit is pushed, use git revert. If it is still local, a reset is fine, and amending is the way to fix the last commit before you push it.
Revert a merge commit and keep the right branch
Say feature/filters was merged into main as 64800d4 and pushed. A plain git revert 64800d4 stops at once:
error: commit 64800d4e065bfc8eaf4dc88fbcbea2610e4c7b12 is a merge but no -m option was given.
fatal: revert failed
A merge commit has two parents, and Git needs to know which side to keep. git log -1 --format=%p 64800d4 prints them: b1cadef 02aef90.
Parent 1 is the branch you merged into, here main. So -m 1 undoes what the feature branch brought:
git revert -m 1 --no-edit 64800d4
[main 943308d] Revert "Merge branch 'feature/filters'"
Date: Sun Nov 8 19:52:23 2026 +0100
1 file changed, 1 deletion(-)
delete mode 100644 src/filters.ts
The git-revert documentation warns that reverting a merge declares you will never want its changes. If you merge feature/filters again later, only the commits made on it since then come in.
Undo the changes of multiple commits at once
In a copy of the repository, three commits added a rate limiter. A range reverts them in one command, one new commit per commit, starting with the newest:
git revert --no-edit HEAD~3..HEAD
git log --oneline -3
9b12608 Revert "feat: add rate limiter"
cb4bdb3 Revert "test: cover rate limiter"
4659100 Revert "docs: document rate limiter"
HEAD~3..HEAD means the last three commits, with HEAD~3 itself left out. To get one commit instead of three, add --no-commit. It applies the changes to your working tree and index (the staging area) without committing them:
git revert --no-commit HEAD~3..HEAD
git commit -m "Revert the rate limiter"
What to do after git revert stops on a conflict
Reverting an older commit can conflict when a later commit edited the same lines. Here, d7b3ca4 raised the search limit to 50, then the pagination commit changed that line to 25:
Auto-merging src/search.ts
CONFLICT (content): Merge conflict in src/search.ts
error: could not revert d7b3ca4... fix: raise search limit to 50
…
Open the file, keep the line you want between the conflict markers, then stage it and continue:
git add src/search.ts
git revert --continue
Git opens your editor with the prepared message; close it to create the revert commit. To give up, git revert --abort returns to the state before the revert. In a range, git revert --skip passes over the commit that conflicts.
How do you get a reverted commit back?
The original commit is still in the history, so revert the revert:
git revert --no-edit b1cadef
[main 8b28eb8] Reapply "feat: cache search results"
…
Git 2.54 titles it Reapply, where older versions such as 2.23 write Revert "Revert "…"".
A commit that left the branch after a reset is different. git reflog lists the previous positions of HEAD, so you can find its hash and go back to it:
943308d HEAD@{0}: reset: moving to HEAD~1
8b28eb8 HEAD@{1}: revert: Reapply "feat: cache search results"
git branch rescue 8b28eb8 puts a branch on it again. Unreachable reflog entries expire after 30 days by default.
Revert a commit in GitMini
GitMini lists revert among its branch operations, next to cherry-pick and branch reset, and its commit graph helps you spot the commit to undo. Every write goes through the Git CLI, so the result is the same new commit as using git revert in a terminal. Git 2.30 or newer must be in your PATH.
If a commit goes missing after a reset, GitMini has an undo, a reflog panel and recovery tools for the operations it supports.
Sources
All consulted on November 8, 2026:
- Git documentation: git-revert (Git 2.54) and git-reflog.
- GitLab Docs: Revert and undo changes.
- GitMini product README.