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 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
How to un-commit last un-pushed git commit without losing the changes - Stack Overflow
This has not been pushed, only committed. ... Save this answer. ... Show activity on this post. ... 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 More on stackoverflow.com
🌐 stackoverflow.com
Is there a way to remove all local commits while keeping the changes?
You can git reset --soft to the last commit you want to keep: https://git-scm.com/docs/git-reset More on reddit.com
🌐 r/git
27
5
February 17, 2022
How do I get back to the most recent push/commit?
git reset HEAD~1 (note that's a tilde). You could also provide the hash of the previous commit as determined by git log -2. This will move any tracked files in the commit to the modified state, and any previously untracked files will be returned to the untracked state. Once you've fixed up the state of your code, you can git push --force to forcefully update upstream. Note this will lose the history of the unfortunate commit. If you don't want to rewrite history, you can perform a git revert HEAD to "undo" the previous commit as a new commit, and then manually fix up the code. This will leave the bunk commit as part of the history. If you know nobody else is working with your repo, rewriting history isn't a huge deal. More on reddit.com
🌐 r/learnprogramming
4
0
August 31, 2022
Top answer
1 of 3
43

If you have already pushed this commit, then it is possible that someone else has pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

git revert <SHA-1>
git push origin branch

Here <SHA-1> is the commit hash of the commit you want to remove. To find this hash value, simply type git log on your branch and inspect the first entry.

Using git revert actually adds a new commit which is the mirror image of the commit you want to remove. It is the preferred way of undoing a commit on a public branch because it simply adds new information to the branch.

If you are certain that you are the only person using this branch, then you have another option:

git reset --hard HEAD~1

followed by

git push --force origin branch

But you should only use this option if no one else is sharing this branch.

2 of 3
8

The way I go about it is by typing git status, which allows us to verify the branch we're currently on, followed by:

git log

Then, you'll see something like this:

commit aa09a82fb69af2d1aebde51d71514f7a03c3a692
Author: User <user@useremail.com>
Date:   Fri Nov 4 15:36:22 2016 -0400

    Fix issue with vertical scroll being forced

commit 411771837efe3ed555395e77fd35105a500ab758
Author: User <user@useremail.com>
Date:   Thu Nov 3 15:50:42 2016 -0400

    Add user notifications

commit f43b262f4e02b5a7268280e1230d44e36d1e547b
Author: User <user@useremail.com>
Date:   Thu Nov 3 12:11:00 2016 -0400

    All your base are belong to us

So, this tells us that commit aa09a82f is your last one, and commit 41177183 is the one before it, then:

git reset --hard 41177183

...brings us back to that commit, retaining the remote backup. With another git status to make sure that everything is all set for the double push (I'm personally a bit obsessive compulsive about verifying my current branch, especially when multitasking):

git push origin :<branch_name>
git push origin <branch_name>

At this point, you should be all set, but it's always good to follow that up with:

git fetch --all --prune
git branch -av

...which cleans up your branch list and shows you both local and remote to compare the commit messages.

Also, if working with a team, make sure that they're aware of this before moving forward. You don't want them to pull or push the branch on their end before you remove the last commit and push.

🌐
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 - However, reverting your code isn’t always straightforward, especially when you’re still learning Git or gaining confidence navigating the command line. In this post, I will walk you through undoing a commit after you push your changes via the terminal. Please note that this post's target audience includes students, early-career developers, or folks who have less experience reverting commits.
🌐
GitHub
gist.github.com › CrookedNumber › 8964442
git: Removing the last commit · GitHub
But garbage collector should remove typically in 30 days or so. This was created thanks to you as a summary of the topic. Thank you all. ... I wanted to uncommit my last commit. so i used git reset --hard HEAD~1 git push origin -f but last commit changes did not seen in my vscode
🌐
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.
Find elsewhere
🌐
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.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - In this section, we will discuss five different scenarios for undoing the last commit in Git, each with a different approach depending on whether you want to keep your changes and staging intact, completely remove the last commit and its changes, or even revert your changes after pushing to a remote repository.
🌐
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   5 days ago
🌐
freeCodeCamp
freecodecamp.org › news › git-remove-last-commit-how-to-undo-a-commit-in-git
Git Remove Last Commit – How to Undo a Commit in Git
September 21, 2022 - Say you made changes to a file, you staged the file with the git add command, and you committed the file with the git commit command. This means that the commit exists only locally and has not been pushed to a remote repository yet.
🌐
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?

🌐
GeeksforGeeks
geeksforgeeks.org › git › how-do-i-delete-unpushed-git-commits
How do I Delete Unpushed Git Commits? - GeeksforGeeks
July 23, 2025 - If you want to keep the changes ... applied by the stash to ensure they are as expected. Note: Be cautious when using git reset --hard as it removes changes irreversibly....
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
🌐
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:
🌐
Better Stack
betterstack.com › community › questions › how-to-delete-unpushed-commits
How Do I Delete Unpushed Git Commits? | Better Stack Community
July 25, 2024 - If you have made commits locally that you no longer want to keep, and they haven't been pushed to any remote repository: Reset the Branch: Use git reset to move the branch pointer to a previous commit, effectively removing the commits from your ...
🌐
Linux Hint
linuxhint.com › remove-git-commit-which-has-not-been-pushed
Remove a Git Commit Which Has Not Been Pushed – Linux Hint
To remove a Git commit that has not been pushed, utilize the “git reset HEAD~1” command or the “git reset --hard HEAD~1” command.
🌐
Tim Mousk
timmousk.com › blog › git-remove-unpushed-commit
How To Remove An Unpushed Commit In Git? – Tim Mouskhelichvili
March 12, 2023 - In Git, HEAD refers to the last commit of the active branch. HEAD~1 refers to the latest commit. Removing all unpushed commits involves syncing the local repository with the remote server.
🌐
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 - See the local commits that are ... fine).” · “I need to surgically keep or drop parts of a commit.” · Prefer git revert <sha> for commits that have already been pushed and shared....
🌐
CodeGenes
codegenes.net › blog › remove-a-git-commit-which-has-not-been-pushed
How to Remove a Git Commit That Hasn’t Been Pushed: Using `git reset --hard` to Undo Local Changes
... # Make changes to fix the commit ... but keep the changes in your working directory (to rework them), use git reset --soft <commit> instead of --hard....
🌐
Reddit
reddit.com › r/git › is there a way to remove all local commits while keeping the changes?
r/git on Reddit: Is there a way to remove all local commits while keeping the changes?
February 17, 2022 -

I would like to organize my PR after my feature is done by removing all of my 'work in progress'-commits and creating new commits that group files to improve the readability of my PR.
I have tried using git reset, but that only unstages changes that have been added using git add . & all files that are already committed seem to be out of reach.