Skip to content
Log In Sign Up info@muvcloud.com

Undo the Last Commit

git revert HEAD

Git works out the reverse of the last commit, applies it, and opens your editor with a message that starts with Revert and the original subject, followed by a line naming the reverted commit. Save and close the editor, and the new commit is made. To keep the suggested message without the editor, add --no-edit.

The editor is usually Vim or nano; see how to save and quit in Vim or how to save and exit in nano.

Revert a Specific Commit

Find the commit's hash, then revert it:

git log --oneline -10
git revert a1b2c3d

Only the changes of that one commit are undone. Later commits stay, as long as they do not depend on the lines you are removing; if they do, you get a conflict.

Revert or Reset?

git revertgit reset
HistoryAdds a commitRemoves commits
Pushed commitsSafeNeeds a force push
Old commitAny commitOnly the latest ones

Use reset for commits that exist only in your clone, and revert for everything that is shared. The reset modes are explained in git reset: soft, mixed, and hard.

Revert Several Commits

git revert a1b2c3d..f6e5d4c

A range A..B covers the commits after A up to and including B; A itself is not reverted. Git makes one revert commit for each commit in the range. To get a single commit instead, stop Git from committing and commit once at the end:

git revert --no-commit a1b2c3d..f6e5d4c
git commit -m "Revert the payment changes"

-n is the short form of --no-commit. The changes are staged, so you can check them with git diff --staged before you commit.

Revert a Merge Commit

A merge commit has two parents, so Git needs to know which side to keep:

git revert -m 1 9f8e7d6

-m 1 keeps the first parent, the branch you were on when you merged (usually main), and undoes what the merged branch brought in. Without -m, Git refuses to revert a merge.

One consequence: Git still considers that branch merged. If you later merge the same branch again, the reverted changes do not come back. To bring them back, revert the revert commit.

Resolve Conflicts

When a later commit changed the same lines, the revert stops with a conflict:

git status
git add config/app.php
git revert --continue

Edit the files that git status lists, remove the conflict markers, add them, and continue. Other options:

  • git revert --abort: cancels the revert and returns to where you started.
  • git revert --skip: skips the current commit in a range and goes on with the next one.

Push the Revert

A revert is an ordinary new commit, so a normal push is enough:

git push

No force push is needed, and teammates receive the revert with their next pull. On a server that deploys from Git, pull the branch as usual; see git pull vs git fetch.

Undo a Revert

If the revert was a mistake and is not pushed yet, git reset --hard HEAD~1 removes it, after you have checked git status for uncommitted work. Once pushed, revert the revert commit with git revert and its hash, which restores the original changes.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations