IF you have NOT pushed your changes to remote

git reset HEAD~1

Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD

This command will revert/remove the local commits/change and then you can push

Answer from Jeril Kuruvila on Stack Overflow
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-git-commit-not-pushed
Remove a Git Commit Which Has Not Been Pushed | Better Stack Community
Amend Commit: git commit --amend - Modify the last commit. These commands will help you remove or alter commits that have not been pushed to the remote repository.
Discussions

How to un-commit last un-pushed git commit without losing the changes - Stack Overflow
What if the repo only has a single commit that's not been pushed? Then git reset HEAD~1 --soft gives the error ambiguous argument 'HEAD~1': unknown revision or path not in the working tree 2020-03-30T16:13:49.067Z+00:00 ... Save this answer. ... Show activity on this post. The easiest way to undo the last ... More on stackoverflow.com
🌐 stackoverflow.com
I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
Start an interactive rebase. git rebase -i HEAD~3 In the editor that opens, change the word pick to edit on the commit you want to change. Save and exit. The rebase will pause at that commit, so you can delete the file. git rm --cached Amend the commit. git commit --amend --no-edit Continue the rebase. git rebase --continue Caveat emptor. More on reddit.com
🌐 r/git
22
25
August 26, 2024
How to undo pushed commits
Yes, it's called a force push. Reset your local branch to the commit you want. Assuming you have main checked out: git reset --hard COMMIT_ID with COMMIT_ID being any ref you want, either a commit id, origin/main^^, etc. Then force push the branch: git push -f origin main To ensure no one else has pushed work on top of main in the meantime, you should use --force-with-lease. git push --force-with-lease origin main (edit): In your case you will probably want to cherry-pick the last commit on the mainline on top of that, unless that's solely related to the commit you want to remove. (edit2): Final note: if you want to avoid people pushing crap you don't want in your mainline, start doing pull requests and deny those people access to your mainline. (edit3): Forgot to mention that you'll have to instruct your coworkers to reset their work too. In this case I'd instruct them to create a separate branch and start a PR with their work, so you (both) have the opportunity to clean things up within that branch before merging to mainline. That avoids them losing their work and/or screwing up their local working tree. More on reddit.com
🌐 r/git
8
5
January 29, 2024
git reset - How can I undo pushed commits using Git? - Stack Overflow
If you are already in the downstream ... to undo the last commit and push the updated history to the remote: git reset --hard HEAD~1 git push -f ... This command will reset your branch to the commit before the last one but keep your changes in the working directory. ... This command will forcefully update the remote repository to match your local repository. ... Helped me remove the commits and push from GitHub. ... You should have a default Git editor that works, you can set it as Notepad++, Visual ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Nobledesktop
blog.nobledesktop.com › learn › git › undo changes in git: git checkout, git revert, & git reset
Undo Changes in Git: checkout, revert, & reset
April 19, 2026 - TIP: Add a number to the end to undo multiple commits. For example, to undo the last 2 commits (assuming both have not been pushed) run Git reset—soft HEAD~2
🌐
LabEx
labex.io › tutorials › git-how-to-undo-a-git-commit-that-has-not-been-pushed-417756
How to undo a Git commit that has not been pushed | LabEx
If you want to undo multiple commits or revert more significant changes, you can use the git reset command. This command allows you to move the branch pointer to a specific commit, effectively undoing all commits that came after that point.
🌐
Aviator
aviator.co › home › blog › how to git undo commit: methods and best practices
How to Git Undo Commit: Methods and Best Practices - Aviator Blog
February 10, 2025 - Check out this guide on How Git compresses files. This undoes the last commit and deletes all changes from the staging area and the working directory. The changes are gone permanently unless they were pushed or backed up. If you’re updating a pricing API, but after committing, you realize the changes were incorrect and should not exist at all.
🌐
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
When you want to undo your last local commit — one that hasn't been pushed to a remote yet — git reset is the right tool:
Published   4 days ago
Find elsewhere
Top answer
1 of 9
1774

There are a lot of ways to do so, for example:

in case you have not pushed the commit publicly yet:

git reset HEAD~1 --soft   

That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. See git reset man


In case you did push publicly (on a branch called 'master'):

git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )

revert commit normally and push

git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master

now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:

git revert --no-commit 86b48ba (hash of the revert commit).

I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master

2 of 9
56

The easiest way to undo the last Git commit is to execute the git reset command with one of the below options

  • soft
  • hard
  • mixed

Let's assume you have added two commits and you want to undo the last commit

$ git log --oneline

45e6e13 (HEAD -> master) Second commit
eb14168 Initial commit

–soft option undo the last commit and preserve changes done to your files

$ git reset --soft HEAD~1


$ git status

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file.html


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

–hard option undo the last commit and discard all changes in the working directory and index

$ git reset --hard HEAD~1


$ git status

nothing to commit, working tree clean


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

--mixed option undo the last commit and keep changes in the working directory but NOT in the index

$ git reset --mixed HEAD~1


$ git status

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file.html

no changes added to commit (use "git add" and/or "git commit -a")


$ git log --oneline

eb14168 (HEAD -> master) Initial commit
🌐
Mazer.dev
mazer.dev › en › git › tips › how-uncommit-undo-last-commit-not-pushed
How to Use Git Uncommit to Revert Changes - Mazer.dev
April 20, 2023 - In this post, we’ll cover how to uncommit in Git. If you mistakenly executed a git commit but haven’t pushed it to the remote repository yet, you can undo the last commit without losing your changes or data.
🌐
Reliable Penguin
blogs.reliablepenguin.com › home › undoing local git commits you haven’t pushed (safely)
Undoing local Git commits you haven’t pushed (safely) Undo Unpushed Git Commits Safely (Exact Commands)
October 10, 2025 - This guide shows exactly how to unwind safely. Choose to discard everything, keep your changes unstaged, or keep them staged for a clean recommit, with copy-pasteable git reset commands for each path.
🌐
Reddit
reddit.com › r/git › i have 3 commits that is not pushed. i need to change the 1st commit. what is the best way to do it?
r/git on Reddit: I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
August 26, 2024 -

I accidentally included a key file in the "doc update" commit so I need to remove it before pushing to the server. However, there are 2 more commits after that one. I know I could cancel the commits and make them all as working tree changes. I just wonder if there is a better way to preserve the last 2 commits.

P.S. this is a personal project so I am the only one who use this. That's why I have 3 commits queued up here. I know its a bad move. If I push my last commit before starting a new one, I could have avoid this situation.

🌐
Reddit
reddit.com › r/git › how to undo pushed commits
r/git on Reddit: How to undo pushed commits
January 29, 2024 -
  • One of our artists managed to push 1 month of progress without merging

  • As you can see I tried to undo the commits one by one in reverse order

  • I couldn't undo the merge commit of Arvincle because it's empty

  • When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone

Is there a way to reset the HEAD of main to the last valid commit?

🌐
OpenReplay
blog.openreplay.com › openreplay blog › undoing git commits after push: safely revert changes on remote repositories
Undoing Git Commits After Push: Safely Revert Changes on Remote Repositories
November 30, 2024 - Before we dive into undoing commits, let’s quickly review how commits work in Git: A commit represents a snapshot of your repository at a specific point in time. Each commit has a unique identifier (SHA-1 hash) and a reference to its parent commit(s). The HEAD pointer refers to the current commit you’re on in your local repository. If you haven’t pushed your last commit yet and want to undo it, you have a few options:
🌐
Medium
rajputankit22.medium.com › remove-git-commit-which-has-not-been-pushed-c95dfb2c3209
Remove git commit which has not been pushed | by Ankit Kumar Rajpoot | Medium
August 29, 2020 - Which should be pushed on Git. Now you should change in one file. Now check the status through the following command. ... Now you should add and commit these changes to the repository through the following commands. // Add file $ git add validator.js// Commit code $ git commit -m "change some validations" ... Now, we will check the status through the following command. ... Now, we will undo the commit.
🌐
DEV Community
dev.to › github › how-to-undo-pushed-commits-with-git-2pe6
How to Undo Pushed Commits with Git - DEV Community
April 6, 2022 - I'm interested in Generative AI, AI Dev Tools, Data Privacy, and Responsible AI ... Undo Pushed Commits in Git With Reset and Revert Undo Pushed Commits With the git reset Command. Undo Pushed Commits With the git revert Command.
🌐
SheCanCode
shecancode.io › home › news & articles › how to undo a commit in github
How to undo a commit in GitHub - SheCanCode
May 27, 2025 - To exit VIM, press `:` to enter ... remote branch: To undo a commit that has not been pushed to a remote repository, use the reset command....
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
Q: What's the difference between git reset --soft HEAD~ and git reset --hard HEAD~? A: git reset --soft HEAD~ undoes the last commit but leaves your changes in your working directory, so you can modify them and re-commit.
Top answer
1 of 16
1910

You can revert individual commits with:

git revert <commit_hash>

This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. Some versions of git also revert the <oldest_commit_hash> itself, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.

To find out the hash of the commit(s) you can use git log.

Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.

Extra note for re-applying reverted commits:

Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.

To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.

2 of 16
870

A solution that keeps no traces of the "undo".

NOTE: Don't do this if someone already pulled your change (I would use this only on my personal repo.)

Run:

git reset <previous label or sha1>

Note: previous means the commit before the erroneous commit

This will re-checkout all the updates locally (so git status will list all updated files, meaning, the files you changed/added/.. and were committed).

Then you "do your work" and re-commit your changes (Note: This step is optional).

git commit -am "blabla"

At this moment your local tree differs from the remote

git push -f <remote-name> <branch-name>

will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).

!! Watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag

Top answer
1 of 15
103

Think we have code.txt file. We make some changes on it and commit. We can undo this commit in three ways, but first you should know what is the staged file... An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

It means if you commit your change, your changes on this file is not saved. You can add this file in your stage with git add code.txt and then commit your change:

Undo last commit:

  1. Now if we want to just undo commit without any other changes, we can use

    git reset --soft HEAD^

  2. If we want to undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use

    git reset --hard HEAD^

  3. And if we want to undo commit and remove changes from stage, we can use

    git reset --mixed HEAD^ or in a short form git reset HEAD^

2 of 15
91

Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. Really, you actually want to redo a commit.

Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.

Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.

NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing means, then you probably haven't pushed. Carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.

That said, here's how to fix your most recent fault commit using a GUI:

  1. Navigate to your repository on the command line and start the GUI with git gui
  2. Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
  3. Now change things to how you want them to look and click Commit.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - So, in merge commits, HEAD~1 and HEAD^ may not behave the same. Yes - the safest option is git revert, because it creates a new commit that undoes the unwanted changes without rewriting shared history. Avoid using git reset on shared branches.
🌐
DEV Community
dev.to › isabelcmdcosta › how-to-undo-the-last-commit--31mg
Git Revert Pushed Commit: How to undo the last commit - DEV Community
February 20, 2018 - If you want to test the previous commit just do git checkout <test commit hash>; then you can test that last working version of your project. If you want to revert the last commit just do git revert <unwanted commit hash>; then you can push ...