Files
30-seconds-of-code/snippets/git/s/undo-commit-without-rewriting-history.md
Angelos Chalaris 9d1f49511a Merge some snippets
2023-05-27 12:53:35 +03:00

1.2 KiB

title, shortTitle, type, language, tags, author, cover, excerpt, dateModified
title shortTitle type language tags author cover excerpt dateModified
Undo a commit in Git Undo commit story git
commit
branch
chalarangelo night-tram Learn the simple way to undo a commit in Git without rewriting history. 2023-05-27T08:23:17+03:00

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.

Revert a commit

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.

# Syntax: git revert <commit>

git revert 3050fc0
# Reverts the commit `3050fc0` and creates a new commit
# with the inverse of its changes

Revert the last commit

The latest commit can be references using the HEAD pointer. So, to revert the last commit, you can simply use git revert HEAD.

# Syntax: git revert HEAD

git revert HEAD
# Reverts the last commit and creates a new commit
# with the inverse of its changes