If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is. This will reset the state of the repository to the previous commit. WARNING: This also discards all your uncommitted local file changes.

Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.

Answer from Ben Jackson on Stack Overflow
🌐
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.

Discussions

version control - How do I undo the most recent local commits in Git? - Stack Overflow
If you want to revert the last ... keep the changes locally that were made in the commit, use this command: ... This command will delete your wrong commit without a Git log. ... There are tons of situations where you really want to undo that last commit into your code. E.g. because you'd like to restructure it extensively - or even discard it ... More on stackoverflow.com
🌐 stackoverflow.com
How do you revert your code back to your last git commit?
If you want to wipe all your uncommitted changes, you can just do git reset --hard head More on reddit.com
🌐 r/learnprogramming
21
1
June 4, 2022
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
Switching branch without commiting changes
🌐 r/git
24
1
July 21, 2024
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
It tells you pretty explicitly how to discard the changes you’ve made. Let’s do what it says: $ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
You can modify history by using git rebase -i. Use this command to modify, squash, and delete commits. # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom.
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - This way, the history of the repo keeps going forward, and other collaborators don't pull a messed-up repo. Let's do a quick summary, since we've covered a lot in this post. Use Git reflog to check commits history. Git stash lets you discard ...
🌐
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
If you'd prefer to unstage the changes but keep them in your working directory, omit the flag (the default is --mixed): $ git reset HEAD~1 · To discard the changes entirely, use --hard — but note this is permanent and cannot be undone: $ ...
Published   6 days ago
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › git › undo the most recent local git commits
Undo the most recent local Git commits | Sentry
The git reset command will return the current branch to a specified previous commit. By default, this command will remove commits from the current branch’s history while leaving the files in the working tree untouched.
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - Which will reset the current branch back two commits and will remove all changes that had been made in the last two commits from the working repository. Warp has a predefined Workflow available that Warp terminal users can call by pressing CTRL-SHIFT-R and starting to type “undo most recent git commit”. One of very many Warp Workflows, this is an easier way to remember the syntax for undoing the last commit while leaving the working tree (the state of the files on disk) untouched:
🌐
Better Stack
betterstack.com › community › questions › how-to-throw-away-local-commits-in-git
How to Throw Away Local Commits in Git | Better Stack Community
June 24, 2024 - Replace <commit> with the commit hash or reference to which you want to reset. This will reset your branch pointer to the specified commit and discard all commits made after it. If you want to keep your changes but don't want them in your current ...
🌐
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
August 12, 2024 - This will include the staged changes in the last commit. Hard Reset: git reset --hard HEAD~1 - Completely remove the last commit and discard changes.
🌐
JetBrains
jetbrains.com › help › idea › undo-changes.html
Undo changes in Git repository | IntelliJ IDEA Documentation
May 5, 2026 - Select a commit you want to discard in the Log view and choose Drop Commit from the context menu. If you have committed changes in several files and now want to roll back some of these changes before pushing your commit, instead of dropping ...
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-delete-local-commit-in-git
How to Delete Local Commit in Git? - GeeksforGeeks
July 23, 2025 - If you want to completely remove the commit and all changes associated with it, you can perform a hard reset: ... `--hard` resets your current branch’s HEAD to the previous commit and discards all changes in the working directory and staging area.
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.
🌐
YouTube
youtube.com › dev leonardo
Undo a Commit on Git - YouTube
If you did a commit but there's still something to add, or you simply want to remove it, here you can find 4 different ways by using amend and reset for diff...
Published   January 12, 2023
Views   1K
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › devops › repos › git › undo
Undo changes in your Git repo - Azure Repos | Microsoft Learn
Unstaged files show up in the Changes section. If the file is in the Changes section, right-click it and choose Undo Changes to discard all changes to the file since the last commit.
🌐
Git
git-scm.com › docs › git-revert
Git - git-revert Documentation
If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1], particularly the --hard option. If you want to extract specific files as they were in another commit, you should see git-restore[1], specifically the --source option.
🌐
Linode
linode.com › docs › guides › how-to-undo-git-commit
Undo a Git Commit: A Step-by-Step Guide | Linode Docs
July 8, 2022 - On branch git-test 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: testfile1.txt
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - git reset HEAD~1 removes the commit and unstages files but keeps changes in the working directory. git reset --hard HEAD~1 removes the commit and discards all changes permanently.
🌐
Simplilearn
simplilearn.com › home › resources › devops › how to revert a commit in git: a step-by-step guide
How to Revert a Commit in Git: A Step-by-Step Guide
June 13, 2026 - Git revert is a command used to undo changes in a Git repository by creating a new commit that reverses the changes made by a previous commit.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - Undoing anything later than your most recent Git action will require the use of either Git revert, Git reset, or Git rebase. While Git revert uses forward change to undo commits, the operation of Git reset is just the opposite.