---
title: "How do I undo the last commit in Git?"  
description: "How do I undo the last commit in Git?"  
author: "Björn Ólafsson"  
published: 2026-11-16  
updated: 2026-11-16  
canonical: https://www.mindstick.com/forum/163040/how-do-i-undo-the-last-commit-in-git  
category: "Git and DevOps"  
tags: ["git", "undo commit", "reset", "revert", "Version Control"]  
reading_time: 1 minute  

---

# How do I undo the last commit in Git?

I committed to the wrong branch and also made a typo in the message. How do I undo or fix the last commit safely?

## Replies

### Reply by Ayesha Achakzai

It depends on what you want and whether you have pushed.

Not pushed yet:

- **Fix only the message:** `git commit --amend -m "Correct message"`
- **Undo the commit, keep the changes staged:** `git reset --soft HEAD~1`
- **Undo the commit, keep the changes unstaged:** `git reset HEAD~1`
- **Undo the commit and throw the changes away:** `git reset --hard HEAD~1` (be careful, this deletes work)

Already pushed to a shared branch:

- Use `git revert <commit-id>`. It creates a new commit that reverses the old one, so nobody's history is rewritten.

For the wrong-branch case: run `git reset --soft HEAD~1`, switch to the right branch with `git switch right-branch`, and commit again. If you panic, `git reflog` shows where HEAD has been, and you can usually recover a "lost" commit from there.


---

Original Source: https://www.mindstick.com/forum/163040/how-do-i-undo-the-last-commit-in-git

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
