How to Delete a Local Commit in Git: A Developer’s Guide

When working with Git, it’s common to make commits you later want to revise, combine, or completely remove. Whether it’s a mistake, a temporary change, or an unwanted history entry, knowing how to delete a local commit in Git gives you more control over your codebase.

In this blog, we’ll walk through different ways to delete a local commit safely and effectively—whether it’s the most recent commit or several commits back.


🧠 Before You Begin: Local vs Remote

This guide focuses on local commits—those not yet pushed to a remote repository like GitHub. Once commits are pushed and shared, deleting them becomes more complex and can affect your team.

⚠️ Important: Use caution when modifying commit history, especially on shared branches.


🛠️ Scenario 1: Delete the Most Recent Commit (Unpushed)

If you just made a commit and want to undo it:

Option A: Keep the changes in your working directory

Use --soft reset:

git reset --soft HEAD~1

✅ This deletes the commit but keeps your changes staged (ready to commit again).

Option B: Keep the changes but unstage them

Use --mixed reset:

git reset --mixed HEAD~1

✅ This removes the commit and unstages the changes, putting them back in your working directory.

Option C: Discard the commit and all changes

Use --hard reset:

git reset --hard HEAD~1

⚠️ This permanently deletes the commit and the code changes.


🛠️ Scenario 2: Delete Multiple Recent Commits

To remove, for example, the last 3 commits:

git reset --hard HEAD~3

Or keep the changes but remove the commits:

git reset --soft HEAD~3

You can adjust the number after HEAD~ depending on how many commits you want to delete.


🛠️ Scenario 3: Delete a Specific Commit (Not the Latest)

Use rebase in interactive mode:

git rebase -i HEAD~N

Replace N with the number of recent commits you want to view. You’ll see something like:

pick 123abc Commit message 1
pick 456def Commit message 2
pick 789ghi Commit message 3

To delete a commit:

  1. Change pick to drop next to the commit you want to remove.
  2. Save and close the editor.

💡 Use rebase only when you’re confident, as it rewrites history.


🛡️ Recover a Deleted Commit (Optional Safety Tip)

If you accidentally delete a commit, you might still recover it (as long as it hasn’t been garbage collected):

git reflog

Find the commit’s reference, then reset back to it:

git reset --hard <commit-hash>

✅ Summary

TaskCommand
Undo last commit (keep changes staged)git reset --soft HEAD~1
Undo last commit (keep changes unstaged)git reset --mixed HEAD~1
Undo last commit (discard changes)git reset --hard HEAD~1
Delete specific commitgit rebase -i HEAD~N
Recover a deleted commitgit reflog + git reset

📌 Best Practices

  • Never use --hard reset unless you’re absolutely sure.
  • Use branches while experimenting to protect main history.
  • Avoid rewriting history on public/shared branches.
  • Always commit and push meaningful, clean changes.

Deleting local commits is a powerful technique when used wisely. Whether you’re refining your history or removing mistakes, Git gives you the tools to shape your commit history the way you need.

Sharing Is Caring:

Leave a Comment