🌐
Git
git-scm.com › docs › git-revert
Git - git-revert Documentation
Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1], particularly the --hard option.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
At the end of the repo setup procedure, we invoke git log to display the commit history, showing a total of 3 commits. With the repo in this state, we are ready to initiate a git revert. 1$ git revert HEAD 2[main b9cd081] Revert "prepend content to demo file" 1 file changed, 1 deletion(-)
🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-revert.html
git-revert(1) Manual Page
June 20, 2025 - Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1), particularly the --hard option.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-back-commit-in-git
How to Back Commit in Git? - GeeksforGeeks
July 23, 2025 - Table of Content · Approach 1: Using 'git revert' Approach 2: Using 'git reset' 'git revert' is the generally preferred approach as it creates a new commit that essentially cancels out the changes introduced by the commit you want to undo.
🌐
Opensource.com
opensource.com › article › 18 › 6 › git-reset-revert-rebase-commands
How to reset, revert, and return to previous states in Git | Opensource.com
If we add a line to a file in each commit in the chain, one way to get back to the version with only two lines is to reset to that commit, i.e., git reset HEAD~1. Another way to end up with the two-line version is to add a new commit that has ...
🌐
Git Tower
git-tower.com › learn › git faq › how to undo, revert, or delete a git commit
How to Undo, Revert, or Delete a Git Commit | Learn Version Control with Git
The same reset technique works for returning to any earlier revision — just provide the commit hash instead of HEAD~1: ... If you only want to restore your whole project to a specific earlier state, read more about how to reset to a previous revision. What if you want to undo the effects of a commit that's not the most recent one — without touching any commits that came after it? That's what git revert is for.
Published   5 days ago
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
It’s safer, because you can revert a revert. # Changed file git commit -am "bug introduced" git revert HEAD # New commit created reverting changes # Next, reapply the reverted commit git log # take hash from the revert commit git revert <rev commit hash> # reverted commit is back (new commit created again)
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › git-reset-revert-tutorial
Git Reset and Revert Tutorial for Beginners | DataCamp
December 16, 2022 - Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit. So, instead of removing a commit, git revert inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content.
🌐
TheServerSide
theserverside.com › tutorial › How-to-git-revert-a-commit-A-simple-undo-changes-example
How to revert a Git commit: A simple example | TheServerSide
The purpose of the git revert command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html ...
🌐
Git
git.github.io › htmldocs › git-revert.html
git-revert(1)
August 2, 2024 - Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1), particularly the --hard option.
🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - Git revert does not delete any commit in this project history. Instead, it inverts the changes implemented in a commit and appends new commits with the opposite effect. This process helps Git remove the unwanted commit from the codebase and retain the history of every commit and the reverted one.
Top answer
1 of 10
111

The safest and probably cleanest way to go is to rebase interactively.

git rebase -i HEAD^^

Or,

git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^

From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.

You can revert a commit with git revert but its going to add more commit messages to the history, which may be undesirable. Use the -n parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.

If the two commits you're working with here affect the same file(s), you may see a merge conflict.

Resetting the repository with git reset --hard should be done with care, as it cannot be undone.

Rewriting history should be done with care.

2 of 10
70

This if from http://nakkaya.com/2009/09/24/git-delete-last-commit/ and it worked for me

Git Delete Last Commit

Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.

If you have committed junk but not pushed,

git reset --hard HEAD~1

HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using --hard any changes to tracked files in the working tree since the commit before head are lost.

If you don't want to wipe out the work you have done, you can use --soft option that will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

git revert HEAD

This will create a new commit that reverses everything introduced by the accidental commit.

🌐
W3Schools
w3schools.com › git › git_revert.asp
Git Revert
git log --oneline e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..." 52418f7 Just a regular update, definitely no accidents here... 9a9add8 (origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html.
Top answer
1 of 7
389

git revert makes a new commit

git revert simply creates a new commit that is the opposite of an existing commit.

It leaves the files in the same state as if the commit that has been reverted never existed. For example, consider the following simple example:

$ cd /tmp/example
$ git init
Initialized empty Git repository in /tmp/example/.git/
$ echo "Initial text" > README.md
$ git add README.md
$ git commit -m "initial commit"
[master (root-commit) 3f7522e] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ echo "bad update" > README.md 
$ git commit -am "bad update"
[master a1b9870] bad update
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example the commit history has two commits and the last one is a mistake. Using git revert:

$ git revert HEAD
[master 1db4eeb] Revert "bad update"
 1 file changed, 1 insertion(+), 1 deletion(-)

There will be 3 commits in the log:

$ git log --oneline
1db4eeb Revert "bad update"
a1b9870 bad update
3f7522e initial commit

So there is a consistent history of what has happened, yet the files are as if the bad update never occurred:

$ cat README.md 
Initial text

It doesn't matter where in the history the commit to be reverted is (in the above example, the last commit is reverted - any commit can be reverted).

Closing questions

do you have to do something else after?

A git revert is just another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you're done.

Do you have to commit the changes revert made or does revert directly commit to the repo?

git revert is a commit - there are no extra steps assuming reverting a single commit is what you wanted to do.

Obviously, you'll need to push again and probably announce your balls-up to the team.

Indeed - if the remote is in an unstable state - communicating to the rest of the team that they need to pull to get the fix (the reverting commit) would be the right thing to do :).

2 of 7
136

Use Git revert like so:

git revert <insert bad commit hash here>

git revert creates a new commit with the changes that are rolled back. git reset erases your Git history instead of making a new commit.

The steps after are the same as any other commit.

🌐
Atlassian
atlassian.com › git › tutorials › resetting checking out and reverting
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
December 16, 2025 - Reverting undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history. For example, the following command will figure out the changes contained in the 2nd to last commit, create a new commit undoing those changes, and tack the new commit onto the existing project. 1git checkout hotfix git revert HEAD~2 ·
🌐
freeCodeCamp
freecodecamp.org › news › git-revert-commit-how-to-undo-the-last-commit
Git Revert Commit – How to Undo the Last Commit
August 31, 2021 - git reset --hard HEAD~1 · This will undo the latest commit, but also any uncommitted changes. In this image, each circle represents a commit. You should really only use reset if the commit being reset only exists locally.
🌐
Gitbybit
gitbybit.com › gitopedia › git-commands › git-revert
Gitopedia: git revert — create an undo commit
git revert creates a new commit that reverses the changes made by the specified commit, effectively canceling out its effects without altering the commit history.
🌐
Reddit
reddit.com › r/git › reverting changes to a single file - why are so simple things so difficult?
r/git on Reddit: Reverting changes to a single file - why are so simple things so difficult?
March 17, 2023 -

I want to revert changes done in a commit to a single file, but not all the other changes which were done on this commit. It seems I can only find the option the revert all the changes done by this commit to all the files. (trying both with ToirtoiseGit and GitKraken)

🌐
Atlassian
atlassian.com › git › tutorials › undoing changes
Undoing Changes in Git | Atlassian Git Tutorial
December 15, 2025 - If we execute git revert HEAD, Git will create a new commit with the inverse of the last commit. This adds a new commit to the current branch history and now makes it look like: 1git log --oneline 2e2f9a78 Revert "Try something crazy" 3872fa7e Try something crazy 4a1e8fb5 Make some important changes to hello.txt 5435b61d Create hello.txt 69773e52 Initial import