← Back to Articles         
Undoing a Local Git Commit Before Push

Undoing a Local Git Commit Before Push

Published on 03 Oct 2026     5 min read Git & GitHub
Scenario Based Questions

Undoing a Local Git Commit Before Push

1. What is it?

If you committed something incorrectly but have not pushed the commit to the remote repository, you can safely undo or modify that local commit.

The most common command is:

git reset

The important question is:

Do you want to undo only the commit, or undo the commit and code changes too?

Git provides different reset modes for this.


2. Simple Syntax / Sample

Suppose your history is:

A --- B --- C
          ↑
         HEAD

C is the incorrect commit and has not been pushed.

Undo commit, keep changes staged

git reset --soft HEAD~1

Undo commit, keep changes but unstage them

git reset HEAD~1

or explicitly:

git reset --mixed HEAD~1

Undo commit and permanently discard changes

git reset --hard HEAD~1

Another common option, if the commit itself is correct but its message or included files need adjustment, is:

git commit --amend

3. Detailed Explanation

Assume you accidentally committed:

git add .
git commit -m "Login changes"

Then you realize something is wrong.

Because you haven't pushed it, you can rewrite your local history without affecting other developers.

Option 1: git reset --soft HEAD~1

git reset --soft HEAD~1

Before:

A --- B --- C
          ↑
         HEAD

After:

A --- B
      ↑
     HEAD

Changes from C → still staged

The commit disappears, but your code changes remain in the staging area.

Check:

git status

You can correct the files and commit again:

git add .
git commit -m "Correct login changes"

This is useful when you want to redo the commit without losing your work.


Option 2: git reset --mixed HEAD~1

git reset --mixed HEAD~1

--mixed is the default, so this also works:

git reset HEAD~1

The commit is removed and the changes remain in your working directory, but they are unstaged.

Commit C
   ↓
Removed

Changes
   ↓
Kept in working directory
   ↓
Not staged

You can then decide exactly what should be committed:

git add LoginService.cs
git commit -m "Fix login service"

This is useful if you accidentally included too many files in the commit.


Option 3: git reset --hard HEAD~1

If you want to completely remove the commit and its changes:

git reset --hard HEAD~1

Before:

A --- B --- C

After:

A --- B

The changes introduced by C are discarded from the working tree.

Warning: --hard can destroy uncommitted work. Use it only when you are certain you no longer need those changes.


4. What If Only the Last Commit Needs Correction?

Sometimes you don't actually need to undo the commit.

For example, you forgot to include a file:

git add ForgotThisFile.cs
git commit --amend

Or change only the last commit message:

git commit --amend -m "Correct login implementation"

Conceptually:

Before:

A --- B --- C

After amend:

A --- B --- C'

Git replaces the previous commit with a new commit because the commit hash changes.

Since the original commit has not been pushed, this is generally straightforward.


5. --soft vs --mixed vs --hard

Command Commit removed? Changes staged? Changes kept?
git reset --soft HEAD~1 Yes Yes Yes
git reset --mixed HEAD~1 Yes No Yes
git reset --hard HEAD~1 Yes No No

A simple way to remember:

--soft
Commit removed
Changes staged
     ↓
Ready to recommit


--mixed
Commit removed
Changes unstaged
     ↓
Ready to edit/select


--hard
Commit removed
Changes removed
     ↓
Work discarded

6. reset vs revert

This distinction is important in interviews.

git reset git revert
Moves branch history backward Creates a new commit that reverses another commit
Can rewrite history Preserves history
Common for local/unpushed commits Preferred for already-pushed/shared commits
--hard can discard work Does not remove existing history
Good when commit hasn't been shared Safer for shared branches

For an unpushed local commit, I would normally consider reset or commit --amend.

For an already pushed/shared commit, I would generally avoid rewriting shared history and use:

git revert <commit-id>

subject to the team's workflow.


7. Key Points

  • If the incorrect commit has not been pushed, rewriting your local history is usually safe.
  • Keep changes staged:
git reset --soft HEAD~1
  • Keep changes but unstage them:
git reset HEAD~1
  • Completely discard the commit and changes:
git reset --hard HEAD~1
  • Correct the latest commit directly:
git commit --amend
  • HEAD~1 means the commit before the current commit.
  • Be especially careful with git reset --hard.
  • Avoid using reset to rewrite commits that other developers may already have pulled.
  • For shared/pushed history, git revert is generally safer.

8. Interview Answer

If I committed something incorrectly but haven't pushed it, I can safely correct my local history. If I want to remove the commit but keep the changes staged, I use git reset --soft HEAD~1. If I want to keep the changes but unstage them, I use git reset HEAD~1. If I want to completely discard both the commit and its changes, I can use git reset --hard HEAD~1, but I would use that carefully. If I only need to fix the latest commit message or add a forgotten file, I would use git commit --amend.

← Back to Articles