Just use this command in your terminal:

git reset --soft HEAD~1 

NOTES:
--soft keeps your changes staged (ready to commit again).
HEAD~1 means “the last commit.” and HEAD~2 means the commit before the last one

🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
... If a file was changed in a commit, and you want to change it back to how it was in the previous commit, but keep the commit history, you can use git revert. The command creates a new commit that reverses all actions taken in the original commit. For example, to remove a file’s changes ...
🌐
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 - These tools allow you to fix issues without permanently losing work. To unstage a file after git add, use git reset HEAD <file-name>. This moves the file back to the working directory but keeps the changes.
Discussions

New to git, advice needed. Revert commit but keep changes
You can either reset, as u/large_crimson_canine suggested, or you can make your new changes, git add them and then amend the commit you already made with: git --amend or, to retain the previous commit message: git --amend --no-edit More on reddit.com
🌐 r/git
17
0
November 18, 2023
How do I undo the last commit without losing changes?
just committed but realized I need to edit some files before finalizing. How can I undo the last commit but keep my changes in the files? More on github.com
🌐 github.com
3
2
Can I delete (or undo) a git commit but keep the changes? - Stack Overflow
Reset your last commit (i.e. the one before fa386d, in our example). Note: It will not delete any changes and keep new and old changes. Removes only the last commit message · git reset 629f9dfb47620a1794f5edf816762f8e30668498 More on stackoverflow.com
🌐 stackoverflow.com
Undoing last commit
You should use git revert in this situation. It will undo the changes made by the faulty commit. Then push the newly created commit to remote. See https://git-scm.com/docs/git-revert for more info. More on reddit.com
🌐 r/git
12
6
January 7, 2021
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 ...
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
If you only want to discard changes to specific files, you can use the following command: ... Replace <file1>, <file2>, and so on with the paths to the files you want to discard changes for.
🌐
Reddit
reddit.com › r/git › new to git, advice needed. revert commit but keep changes
r/git on Reddit: New to git, advice needed. Revert commit but keep changes
November 18, 2023 -

Hello everyone! I'm working on a project in a team. I'm using GUI git client Fork, while also learning command line git.
I finished a task and made a commit (not pushed), but soon realized that I didn't get it done properly, and that I needed to put more work into it. So I left my initial commit as it was, and just kept on working on the task. Right now I'm finally finishing it, but I'm asked to make only one commit with the whole task in it.

When I try to Revert the initial commit, it gives me an error:

Your local changes to the following files would be overwritten by merge
Revert failed

What should I do to keep the changes in the initial commit and reapply the new ones? I may have rewritten some code from the initial commit, but not fully, so some parts must stay, and the new changes to be added.

Thank you for your attention!

🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
... 2. Undo the last commit but keep the changes: If you want to undo the commit but keep the changes in your working directory, use the git reset command with the --soft flag, followed by HEAD~.
Find elsewhere
Top answer
1 of 14
3396

It's as simple as this:

git reset HEAD^

Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" or use HEAD~1 in those cases.

git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

Note that another option is to carry on as normal, and then at the next commit point instead run:

git commit --amend [-m … etc]

which will instead edit the most recent commit, having the same effect as above.

Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that

2 of 14
430

There are two ways of handling this. Which is easier depends on your situation.

Reset

If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset

git reset HEAD^

Takes your branch back to the commit just before your current HEAD. However, it doesn't actually change the files in your working tree. As a result, the changes that were in that commit show up as modified - its like an 'uncommit' command. In fact, I have an alias to do just that.

git config --global alias.uncommit 'reset HEAD^'

Then you can just used git uncommit in the future to back up one commit.

Squashing

Squashing a commit means combining two or more commits into one. I do this quite often. In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message.

git rebase -i <ref>

I say above because I want to make it clear this could be any number of commits back. Run git log and find the commit you want to get rid of, copy its SHA1 and use it in place of <ref>. Git will take you into interactive rebase mode. It will show all the commits between your current state and whatever you put in place of <ref>. So if <ref> is 10 commits ago, it will show you all 10 commits.

In front of each commit, it will have the word pick. Find the commit you want to get rid of and change it from pick to fixup or squash. Using fixup simply discards that commits message and merges the changes into its immediate predecessor in the list. The squash keyword does the same thing, but allows you to edit the commit message of the newly combined commit.

Note that the commits will be re-committed in the order they show up on the list when you exit the editor. So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them.

WARNING:

Rebasing modifies history - DONT do this to any commits you have already shared with other developers.

Stashing

In the future, to avoid this problem consider using git stash to temporarily store uncommitted work.

git stash save 'some message'

This will store your current changes off to the side in your stash list. Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. You can also simply run git stash and nothing else, but no message will be stored.

You can browse your stash list with...

git stash list

This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@{#} where # is its position in the array of stashes.

To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run...

git stash apply stash@{#}

Again, there # is the position in the array of stashes. If the stash you want to restore is in the 0 position - that is, if it was the most recent stash. Then you can just run the command without specifying the stash position, git will assume you mean the last one: git stash apply.

So, for example, if I find myself working on the wrong branch - I may run the following sequence of commands.

git stash
git checkout <correct_branch>
git stash apply

In your case you moved around branches a bit more, but the same idea still applies.

🌐
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 - Regularly communicate with your team to avoid conflicts when undoing commits on shared branches. ... Use `git revert -m 1 <merge-commit-hash>` to revert a merge commit, specifying the parent number (`-m 1`) to keep.
🌐
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: ... This rewinds the branch pointer by one commit. The --soft flag preserves your changes as staged modifications, ...
Published   6 days ago
🌐
Medium
medium.com › @sivaraaj › how-to-undo-the-most-recent-local-commits-in-git-7892fd717964
How to Undo the Most Recent Local Commits in Git ? | by Sivaraj Ramasamy | Medium
February 18, 2024 - $ git log --oneline a1b2c3d (HEAD -> main) Add new feature e4f5g6h Initial commit $ git reset --hard HEAD~1 HEAD is now at e4f5g6h Initial commit $ git status On branch main nothing to commit, working tree clean $ git log --oneline e4f5g6h (HEAD ...
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-the-most-recent-local-commits-in-git
How to Undo the Most Recent Local Commits in Git? - GeeksforGeeks
July 23, 2025 - This example undoes the last three commits, keeping the changes staged. The `git revert` command is a safer alternative, especially when working with a shared repository, as it creates a new commit that undoes the changes from the specified commit.
🌐
Jessica Temporal
jtemporal.com › undoing-the-last-commit-and-reusing-the-message
Undoing the last commit and keeping the changes for a next commit | Jessica Temporal
June 20, 2026 - Given this scenario, the first step is to use the command git reset. Maybe you don’t know that there is a flag that while undoing a commit with git reset, it allows you to keep the commit changes on staging and the commit message stored in ...
🌐
GitHub
gist.github.com › wilsonsilva › c6f870e6423a1c0076224d1f1e468dbb
Undo last commit but keep changes - Gist - GitHub
Copy link · Copy Markdown · Thank you · Copy link · Copy Markdown · @Arman-Ghazaryan something like · git revert --no-commit abc123 · See git revert · Copy link · Copy Markdown · Awesome!
🌐
DataCamp
datacamp.com › tutorial › git-revert-last-commit
Git Revert Last Commit: How to Safely Undo a Change in Git | DataCamp
July 8, 2025 - If you need to reapply a change, consider using git cherry-pick instead. Use reflog to recover if things go sideways: Made a mistake mid-revert? Use git reflog to view your recent actions and recover lost states. If you're collaborating in a professional setting, git revert to the last commit using git revert HEAD helps maintain transparency while reducing the risk of accidental data loss. Whether you're cleaning up a mistake or rolling back a bug, revert keeps ...
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
2.4 Git Basics - Undoing Things
$ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README · You can see that the changes have been reverted. If you would like to keep the changes you’ve made to that file but still need to get it out of the way for now, we’ll go over stashing and branching in Git Branching; these are generally better ways to go.
🌐
Linode
linode.com › docs › guides › revert-last-git-commit
How to Revert the Last Commit in Git | Linode Docs
February 15, 2024 - Here, the command is used with the --oneline option to make each commit display on a single line: ... f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file.
🌐
LabEx
labex.io › tutorials › git-how-to-revert-a-git-commit-without-losing-changes-415168
How to revert a Git commit without losing changes | LabEx
The --no-commit (or -n) option is perfect for this. First, let's reset our repository to the state before our last revert, so we can try a different approach. We will use git reset for this.
🌐
GitHub
gist.github.com › vishaltelangre › 5531806
Revert last local or remote commit (git) - Gist - GitHub
git reset --soft HEAD^ # use --soft if you want to keep your changes git reset --hard HEAD^ # use --hard if you don't care about keeping the changes you made · Now git log will show that our last commit has been removed.
🌐
DEV Community
dev.to › andyrewlee › how-to-undo-last-commit-and-keep-changes-1eh2
How to Undo Last Commit and Keep Changes - DEV Community
April 30, 2020 - To undo the last commit but keep the changes, run the following command: git reset --soft HEAD~1...
🌐
GitHub
github.com › orgs › community › discussions › 183621
undo the last commit · community · Discussion #183621
January 6, 2026 - Something went wrong. There was an error while loading. Please reload this page. ... You can use git reset with the --soft or --mixed option to undo the last commit while keeping your changes.