snippetjavascriptTip
Undo a commit in Git
Viewed 0 times
undogitcommit
Problem
It's not uncommon to make a mistake when committing changes to a repository. When you realize something went wrong, you might not be able to rewind the changes you made, especially if you've already pushed them to a remote repository. In that case, you'll want to undo the commit, without rewriting history.
As you might have guessed,
The latest commit can be references using the
As you might have guessed,
git revert is the command you're looking for. Using this command, you can revert a commit, creating a new commit with the inverse of the commit's changes.The latest commit can be references using the
HEAD pointer. So, to revert the last commit, you can simply use git revert HEAD.Solution
# Syntax: git revert <commit>
git revert 3050fc0
# Reverts the commit `3050fc0` and creates a new commit
# with the inverse of its changesThe latest commit can be references using the
HEAD pointer. So, to revert the last commit, you can simply use git revert HEAD.Code Snippets
# Syntax: git revert <commit>
git revert 3050fc0
# Reverts the commit `3050fc0` and creates a new commit
# with the inverse of its changes# Syntax: git revert HEAD
git revert HEAD
# Reverts the last commit and creates a new commit
# with the inverse of its changesContext
From 30-seconds-of-code: undo-commit-without-rewriting-history
Revisions (0)
No revisions yet.