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
version control - How do I undo the most recent local commits in Git? - Stack Overflow
Yes, in the case we need to just fix the commit, we shoud not use --hard`, but leave it with the default --soft` so we can remove and add stuff before the ``git push -f` 2021-03-11T16:29:35.097Z+00:00 ... Reset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to Undo, Revert, or Delete a Git Commit
To undo the last local commit (one that hasn't been pushed yet) while keeping your changes staged, run git reset --soft HEAD~1. To unstage the changes but keep the edits in your working directory, use git reset --mixed HEAD~1. To discard the changes entirely, use git reset --hard HEAD~1 — this permanently deletes the uncommitted work. To undo a specific older commit without altering history, use git revert , which creates a new commit that applies the reverse of the targeted commit's changes; this is the safest approach for shared branches. The --no-commit flag stages the reverting changes wit
🌐
git-tower.com
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 ...
What happens if I encounter conflicts while reverting a commit?
Resolve the conflicts manually, stage the changes, and then run `git revert --continue` to complete the revert operation.
🌐
blog.openreplay.com
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 ...
🌐
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   17 hours ago
🌐
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.
Find elsewhere
🌐
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.
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.
🌐
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.
🌐
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....
🌐
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?

🌐
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 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 ...
🌐
Redirect
sethrobertson.github.io › GitFixUm
On undoing, fixing, or removing commits in git
If you see this message, your browser is broken. Please click here
🌐
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.