How to cancel a local git commit?
Categories:
How to Cancel a Local Git Commit: A Comprehensive Guide

Learn various methods to undo or modify your last local Git commit, from soft resets to amending commits, ensuring your repository history remains clean.
Accidentally committed too early? Made a typo in your commit message? Included a file you didn't mean to? Don't worry, it happens to everyone. Git provides powerful tools to modify your local commit history before you push it to a remote repository. This article will guide you through the most common scenarios for canceling or modifying a local commit, helping you maintain a clean and accurate project history.
Understanding Git Reset: The Core Tool
The git reset
command is your primary tool for undoing changes in Git. It's crucial to understand its different modes, as they determine how your working directory and staging area are affected. The key is to use git reset
before pushing your changes to a shared remote repository. Once pushed, altering history becomes more complex and can disrupt collaborators.
flowchart TD A[Start: Commit Made] --> B{What do you want to undo?} B -->|Only commit message/files| C[git commit --amend] B -->|Commit + staging area| D[git reset --soft HEAD~1] B -->|Commit + staging + working dir| E[git reset --mixed HEAD~1] B -->|Commit + staging + working dir + untracked| F[git reset --hard HEAD~1] C --> G[End: Commit Modified] D --> G E --> G F --> G
Decision flow for choosing the right Git reset command.
Scenario 1: Modifying the Last Commit (Before Push)
Often, you just need to tweak the last commit. This could be fixing a typo in the commit message, adding a forgotten file, or removing an accidentally staged file. The git commit --amend
command is perfect for this.
# 1. Make your changes (e.g., fix a typo, add a file)
# 2. Stage the changes (if adding/modifying files)
git add forgotten_file.txt
# 3. Amend the last commit
git commit --amend
Using git commit --amend
to modify the last commit.
This command will open your configured text editor, allowing you to change the commit message. If you staged any new changes before running git commit --amend
, those changes will also be added to the previous commit. The old commit will be replaced by the new, amended one, effectively rewriting history for that single commit.
git commit --amend -m "New commit message"
to bypass the editor.Scenario 2: Undoing the Last Commit (Keeping Changes)
Sometimes you want to undo the commit itself but keep all the changes you made in your working directory and staging area. This is where git reset --soft HEAD~1
comes in handy. HEAD~1
refers to the commit directly before the current HEAD.
# Undo the last commit, but keep changes staged
git reset --soft HEAD~1
# Now your changes are staged, and you can commit again
git status
Using git reset --soft
to uncommit while preserving staged changes.
After this command, your working directory will remain untouched, and all the changes from the undone commit will be moved back to the staging area. You can then make further modifications, stage different files, or create a new commit with a different message.
Scenario 3: Undoing the Last Commit (Unstaging Changes)
If you want to undo the commit and also unstage the changes, moving them back to your working directory (untracked or modified but not staged), use git reset --mixed HEAD~1
. This is the default behavior of git reset
if no mode is specified.
# Undo the last commit and unstage changes
git reset --mixed HEAD~1
# Now your changes are in the working directory, not staged
git status
Using git reset --mixed
to uncommit and unstage changes.
This is a very common scenario when you realize the entire commit was a mistake, and you want to start fresh with those changes. Your files will still be there, but they won't be staged for the next commit.
Scenario 4: Completely Discarding the Last Commit and Changes
Be extremely cautious with this option! git reset --hard HEAD~1
will undo the last commit and discard all changes in your working directory and staging area that were part of that commit. This means any uncommitted changes will be lost permanently.
# DANGER: This will discard all uncommitted changes and the last commit!
git reset --hard HEAD~1
Using git reset --hard
to completely discard the last commit and its changes.
git reset --hard
if you are absolutely certain you want to discard all recent changes and the last commit. There is no undo for git reset --hard
once executed, unless you know the exact commit hash you were on before the reset (which can be found using git reflog
).Recovering from a Hard Reset (Using Git Reflog)
If you accidentally performed a git reset --hard
and lost work, git reflog
can be your savior. The reflog records every change to your HEAD, allowing you to find previous states of your repository.
# View the reflog to find previous HEAD positions
git reflog
# Example output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# e4f5g6h HEAD@{1}: commit: My accidental commit
# i7j8k9l HEAD@{2}: commit (initial): Initial commit
# To go back to the state before the reset (e.g., 'e4f5g6h')
git reset --hard e4f5g6h
Using git reflog
to recover from a git reset --hard
.
The git reflog
command shows a history of where your HEAD has been. You can then use git reset --hard <commit_hash>
with the hash from the reflog to restore your repository to a previous state.