How to Undo the Last Commit in Git: Complete Step-by-Step Guide for Beginners

Meta description

Learn how to undo the last commit in Git safely. Step-by-step guide with examples, commands, and common mistakes explained clearly.

If you're learning Git, you may also want to understand how to undo mistakes:


Introduction

Developer undoing last commit in Git using reset and revert commands


If you’ve ever made a mistake in Git, you’re not alone. One of the most common problems developers face is figuring out how to undo the last commit in Git without breaking their project.

Maybe you committed the wrong file, forgot to include something important, or simply realized something needs to be changed. The good news is that Git gives you several ways to fix this—safely and efficiently.

In this complete guide, you’ll learn exactly how to undo your last commit, when to use each method, and how to avoid common mistakes that can cause data loss.




Search Intent & Keywords (for SEO clarity)

Main keyword:

how to undo the last commit in git

Long-tail variations:

undo last git commit without losing changes
git remove last commit local
git reset last commit explained
how to undo commit before push git
git revert vs reset difference


What Is a Commit in Git? (Quick Explanation)

Diagram explaining how a Git commit works as a snapshot of project changes


Before undoing a commit, it’s important to understand what it actually is.

A commit in Git is like a snapshot of your project at a specific moment. Every time you commit, you save:

changes to files
a message describing those changes
a reference in your project history

Think of it like a “save point” in a game.

When Do You Need to Undo a Commit?

You might need to undo a commit when:

❌ You added the wrong files
❌ You forgot to include important changes
❌ Your commit message is incorrect
❌ You made a mistake before pushing


How to Undo the Last Commit in Git (Step-by-Step)

There isn’t just one way to undo a commit. The right method depends on what you want to keep or remove.


🟢 Method 1: Undo Last Commit (Keep Changes in Files)

This is the safest and most common method.

git reset HEAD~1

✅ What this does:

Removes the last commit

Keeps your changes in your working directory

💡 When to use:

You want to fix something and recommit

🟢 Method 2: Undo Last Commit (Keep Changes Staged)

git reset --soft HEAD~1


✅ What this does:

Removes the last commit

Keeps changes staged (ready to commit again)

💡 When to use:

You just want to edit the commit message
You want to recommit quickly


🔴 Method 3: Undo Last Commit (Delete Everything)

git reset --hard HEAD~1

⚠️ What this does:

Removes the commit
Deletes all changes permanently

🚨 Warning

This cannot be undone easily.

💡 When to use: 

You want to completely discard changes


🟡 Method 4: Undo Commit After Push (Safe Method)

git revert HEAD


✅ What this does:

Creates a new commit that reverses the previous one Keeps project history intact

When to use:

You already pushed to a shared repository


⚖️ Git Reset vs Git Revert (Key Difference)



Feature             git reset                  git revert

Removes                 yes                          no
commit history

Safe for shared       no                         yes
repos                                                 

Keeps 
history clean          no                           yes

Recommended       ❌                            ✅            after push


👉 Simple rule:

Use reset → before push
Use revert → after push


🧠 Understanding HEAD~1 (Important Concept)

In all commands, you see:

HEAD~1

This means:

HEAD → current commit
~1 → one commit before

Examples:

HEAD~1 → last commit
HEAD~2 → two commits ago




👉 Understanding branches is important, but knowing when to use different commands is essential:

https://techfutureglobal.blogspot.com/2026/03/how-to-undo-pushed-commits-in-git-safe.html

🛠️ Practical Example (Real Scenario)

Step-by-step Git workflow showing how to undo a commit using reset command


Let’s say you:

1.Add files
2.Commit
3.Realize something is wrong


Fix it like this:

git reset HEAD~1

Now:

your files are still there
you can edit them
commit again correctly


🚨 Common Mistakes to Avoid

Illustration of common Git mistakes including misuse of reset hard command


❌ 1. Using --hard without understanding

You might lose all your work.


❌ 2. Reset after push

⚠️ Using git reset --hard will permanently delete your changes.

This breaks history for others.


❌ 3. Not checking status

Always run:

git status

Before and after changes.


❌ 4. Forgetting backups

If unsure, create a branch:

git branch backup


💡 Pro Tips (Advanced but Simple)

Use git log to check history

git log --oneline


Use git reflog to recover commits

git reflog

Even deleted commits can sometimes be recovered.

Work in small commits

Smaller commits = easier fixes.


Best Practice Summary



Undo safely with git reset before push

Use git revert after push

Avoid --hard unless necessary

Always check your status


Conclusion

Learning how to undo the last commit in Git is one of the most important skills for any developer. Mistakes happen all the time—but Git gives you powerful tools to fix them safely.

The key is choosing the right method:

Use git reset when working locally

Use git revert when changes are already shared





Call to Action

Next time you make a mistake in Git, don’t panic. Use the methods in this guide to fix your commits quickly and confidently—and keep your workflow clean and efficient.