How to Undo Your Last Git Commit (Not Pushed)
git reset command. This allows you to revert the commit. You control whether you preserve the changes in your working directory, move them to the staging area, or discard them completely.Understanding how to safely undo changes is a fundamental skill for any developer. This is especially true when you work on local branches before sharing your work. Mastering git reset prevents many common frustrations.
Understanding Git’s State: Working Directory, Staging Area, and Local Repository
Before diving into the commands, it’s important to grasp the three main states Git tracks your files in: the working directory, the staging area (or index), and the local repository (HEAD).
The working directory is where you make changes to your files. These are the actual files you see and edit on your file system. Changes here are not yet tracked by Git in a commit.
The staging area acts as a pre-commit buffer. When you run git add, you move changes from your working directory to the staging area. This marks them for inclusion in the next commit. This allows you to craft precise commits, including only relevant changes.
The local repository, specifically the HEAD pointer, points to the tip of your current branch. Each commit creates a snapshot of your staged changes and moves HEAD forward. When you undo a commit, you are essentially moving this HEAD pointer back to an earlier state.

We always recommend visualizing these states; it clarifies what each Git command truly affects. When you commit, you’re taking a snapshot of the staging area and adding it to your repository history. When you need a git undo last commit not pushed locally, you’re essentially telling Git to forget that last snapshot and potentially move the files back into the staging area or working directory.
The Core Command: git reset for Unpushed Commits
The git reset command is your primary tool for undoing local commits. It has different modes that dictate how your working directory and staging area are affected. For an unrushed commit, you’re typically looking to move the HEAD pointer back without affecting the remote history.

The general syntax to undo the last commit is git reset -- HEAD~1. The HEAD~1 part tells Git to move the HEAD pointer back one commit from its current position. You can also use the specific commit hash if you need to revert to an earlier, non-immediate parent commit.
It is helpful to always confirm the state of your repository with git status before and after running reset commands to ensure the outcome matches the expectation.
How to Undo the Last Commit and Keep Changes Staged (git reset --soft)
If you need a git undo last commit not pushed solution to keep all the changes from that commit in your staging area, ready to be recommitted, use git reset --soft HEAD~1. This is useful if you committed too early, made a typo in the commit message, or realized you need to add more changes to that commit.
-
- Check your current commit history:
git log --onelineThis helps you confirm the commit hash of the commit you intend to undo.
- Execute the soft reset:
git reset --soft HEAD~1This command moves the HEAD pointer back one commit. The changes from the undone commit are now in your staging area.
- Verify the state:
git statusYou should see the files that were part of the undone commit listed as ‘Changes to be committed’.
- Recommit or modify: You can now amend the commit message using
git commit --amend -m "New commit message", add more changes and then commit, or simply commit again with a new message.
- Check your current commit history:
This mode is non-destructive to your actual file content and is generally the safest option when you need a git undo last commit not pushed locally.
How to Undo the Last Commit and Keep Changes in Working Directory (git reset --mixed)
The git reset --mixed HEAD~1 command (which is also the default behavior if you omit --mixed) undoes the last commit and unstages the changes, moving them back into your working directory. This means the files themselves are unchanged, but Git no longer considers them staged for the next commit. You’ll need to git add them again if you want to commit them.
- Check your current commit history:
git log --oneline - Execute the mixed reset:
git reset HEAD~1(or
git reset --mixed HEAD~1). This moves HEAD back and unstages the changes. - Verify the state:
git statusYou should see the files that were part of the undone commit listed as ‘Changes not staged for commit’.
- Re-add and recommit: You can now modify the files, then use
git add .to stage them again, and finallygit commit -m "New commit message".
This mode is used frequently when there is a need for a git undo last commit not pushed because a commit contained too many unrelated changes and it is desirable to break it down into smaller, more focused commits.
How to Undo the Last Commit and Discard All Changes (git reset --hard)
The git reset --hard HEAD~1 command is the most drastic. It helps you for git undo last commit not pushed. It also removes the changes from the staging area and discards all changes in your working directory. This means you will permanently lose any modifications made in that last commit unless you backed them up elsewhere.
According to Tech-Insider.org, commands like
git restorecan be important for recovering files after significant changes or rewrites. Whilegit reset --harddiscards changes,git restoreoffers precision in managing file states.
We advise extreme caution when using --hard. Only use it if you are absolutely certain you want to discard all changes from the last commit and reset your working directory to the exact state of the previous commit.
- Check your current commit history:
git log --oneline - Execute the hard reset:
git reset --hard HEAD~1Warning: This will permanently delete any uncommitted changes and revert your working directory to the state of the commit before the last one.
- Verify the state:
git statusYour working directory should be clean, and there should be no pending changes.
This command lets you git undo last commit not pushed, discarding any work that followed the target commit.
Git undo last commit not pushed for specific files
Sometimes you might only want to undo the changes to specific files. These changes were part of the last commit. You might not want to undo the entire commit. This is where git restore becomes useful, especially for changes you haven’t pushed.
If you want to revert a specific file in your working directory to its state in the previous commit, you can use:
git restore --source=HEAD~1 <file_path>
This command will fetch the version of <file_path> from the commit before HEAD (HEAD~1) and place it directly into your working directory, discarding any changes you made to that file in the last commit (and any uncommitted changes).
If you committed the file and want to unstage it, move it from the staging area back to the working directory. Do this without affecting the actual file content. You’d use:
git restore --staged <file_path>
This is equivalent to git reset HEAD <file_path>. It is useful if you accidentally staged a file you didn’t intend to commit. git restore is considered to be a more intuitive and safer command for manipulating individual files. Using git reset with file paths can sometimes be confusing.
What if the Commit is on GitHub (Pushed to Remote)?
The methods described above (git reset) are exclusively for commits you have not yet pushed to a remote repository like GitHub. Once you push a commit, using git reset to rewrite history can cause significant problems for collaborators.

If you need a git undo last commit not pushed method, do not use git reset when it is already on GitHub. Instead, the correct approach is to use git revert. The git revert command creates a new commit. This new commit undoes the changes a previous commit introduced. It’s a non-destructive operation that maintains a clean, linear history.
To revert the last pushed commit:
- Identify the commit hash:
git log --onelineFind the hash of the commit you want to revert.
- Execute the revert command:
git revert <commit_hash>This will open your default editor to allow you to write a commit message for the revert commit. Save and close the editor.
- Push the new revert commit:
git push origin <your_branch>
git revert is safe because it doesn’t rewrite history; it simply adds a new commit to git undo last commit not pushed. This is important for collaborative environments to avoid disrupting other developers’ work.
Common Mistakes and Edge Cases
When undoing commits, several scenarios can lead to confusion or unintended consequences. These have been encountered repeatedly.

Undoing Multiple Commits
If you need to undo more than just the last commit, you can adjust the HEAD~N notation. For example, to undo the last three commits:
git reset --soft HEAD~3: Undo three commits, keep changes staged.git reset --mixed HEAD~3: Undo three commits, upstaging changes.git reset --hard HEAD~3: Undo three commits, discarding all changes.
Always be extra careful when specifying N, especially with --hard, as you could lose substantial work.
Accidentally Using git reset --hard
If you accidentally use git reset --hard and lose changes, there’s a slim chance of recovery using git reflog. The reflog records every change to HEAD. You can find the commit hash before your destructive reset and then reset to that commit.
- View the reflog:
git reflogLook for the entry that corresponds to the state before your unwanted reset. It will often be labeled something like ‘HEAD@{1}: commit: your commit message’ or ‘HEAD@{2}: reset: moving to HEAD@{1}’.
- Reset to the desired state:
git reset --hard <commit_hash_from_reflog>Replace
<commit_hash_from_reflog>with the hash you identified in the reflog.
This is your last resort to git undo the last commit not pushed.
Detached HEAD State
If you need to git undo last commit not pushed by resetting to a specific commit hash that isn’t the tip of a branch, you might enter a ‘detached HEAD’ state. This means your HEAD points directly to a commit, not to a branch name. Any new commits you make in this state won’t be part of any branch. You can lose them once you switch branches.
To avoid this, always create a new branch from your detached HEAD state before making new commits:
git checkout -b <new_branch_name>
Then, you can merge this new branch back into your main development branch later if needed. It is preferable to reset to a branch name (e.g., git reset --soft main) or HEAD~N to stay on a branch.
Who This Advice Does Not Suit
The advice regarding git reset is primarily for individual developers working on their local branches or in scenarios where they are absolutely sure no one else has pulled their recent commits. It is not suitable for:

- Commits already pushed to a shared remote repository: As mentioned,
git resetrewrites history, which can cause significant issues for collaborators who have already pulled that history. In shared environments, always usegit revertfor pushed commits. - Public branches in open-source projects: Rewriting history on public branches is generally frowned upon. It can break the workflow for many contributors.
- Developers unfamiliar with Git’s internal workings: If you’re not comfortable with the concepts of HEAD, index, and working directory, starting with
git revert(even for local commits) might be a safer, albeit less flexible, initial approach as it’s non-destructive.
My advice is always to err on the side of caution. If in doubt about whether a commit has been shared, assume it has and use git revert.