How to Delete a Commit in GitHub

Summarize this article with:

You just pushed a commit with your API key exposed. Or maybe you committed to the wrong branch. It happens.

Knowing how to delete a commit in GitHub saves you from these moments of panic.

The process differs depending on whether your commit exists only locally or has already been pushed to a remote branch.

This guide covers three methods: git reset for local commits, force push for remote cleanup, and interactive rebase for surgical history rewrites.

You will learn exact commands, when to use each approach, and how to recover if something goes wrong.

Takes about 5-10 minutes. Basic version control knowledge required.

How to Delete a Commit in GitHub

maxresdefault How to Delete a Commit in GitHub

Deleting a commit in GitHub is the process of removing one or more snapshots from your Git repository history using terminal commands.

Users need this when they accidentally commit sensitive data, push to the wrong branch, or want to clean up messy commit history.

This guide covers 3 methods requiring 5-10 minutes and basic Git command knowledge.

Why is GitHub the heart of open source?

Uncover GitHub statistics: developer community growth, repository trends, collaboration patterns, and the platform that powers modern software development.

Explore GitHub Data →

Prerequisites

Before you start, make sure you have:

  • Git version 2.x or later installed
  • Terminal or Git Bash access
  • A repository with existing commit history
  • Push access to the remote repository (for remote deletions)
  • 5-10 minutes

How Do You Delete the Most Recent Local Commit?

Run git reset HEAD~1 in your terminal to remove the last commit from your local branch.

The commit disappears from git log output, but your files may or may not change depending on which reset flag you use.

What Does git reset –soft Do?

The soft reset removes the commit but keeps all changes in the staging area.

Your files stay exactly as they were; only the commit record is gone.

Use this when you want to redo a commit with a different message or combine changes.

What Does git reset –hard Do?

The hard reset permanently deletes the commit and all associated file changes.

Your working directory reverts to the previous commit state.

Gone forever. No recovery without git reflog.

What Does git reset –mixed Do?

Mixed is the default behavior when you run git reset without flags.

Removes the commit, unstages changes, but keeps modified files in your working directory.

How Do You Delete a Commit From Remote Repository?

First delete the commit locally with git reset --hard HEAD~1, then run git push origin branch-name --force to update the remote.

The remote branch now matches your local history with the commit removed.

When Should You Use Force Push?

Only force push when you are the sole contributor or have coordinated with your team.

Force pushing rewrites shared history and can break other developers’ local repositories.

Always communicate before running this command on collaborative branches.

What Is the Difference Between –force and –force-with-lease?

The --force flag overwrites remote history regardless of what others have pushed.

The --force-with-lease flag checks if anyone else pushed commits since your last fetch; if so, it fails safely.

Use –force-with-lease on shared branches to prevent accidentally destroying teammates’ work.

How Do You Delete a Specific Commit From History?

Run git rebase -i HEAD~N where N is the number of commits back you want to view, then change pick to drop for the commit you want to remove.

The interactive rebase rewrites history without that commit.

How Do You Find the Commit Hash?

Run git log --oneline to see a compact list of commits with their SHA-1 hash identifiers.

Copy the 7-character hash of the commit just before the one you want to delete.

What Does Interactive Rebase Editor Show?

The editor displays each commit on its own line starting with the word pick.

Available commands: pick (keep), drop (delete), squash (combine), reword (change message).

Save and close the editor to execute your changes.

How Do You Undo a Commit Without Changing History?

Run git revert commit-hash to create a new commit that reverses the changes from the specified commit.

The original commit stays in history; git revert simply adds a new commit that undoes its effects.

Safe for shared branches because it preserves the existing timeline.

When Should You Choose Revert Over Reset?

Use revert when others have already pulled the commit you want to undo.

Reset rewrites history; revert adds to it. Team projects need revert.

How Do You Delete Multiple Commits at Once?

Use git reset --hard HEAD~N where N equals the number of commits to remove, or run interactive rebase with git rebase -i HEAD~N and mark multiple commits as drop.

Both methods work; reset is faster for consecutive commits, rebase gives more control over which specific commits to keep or squash.

How Do You Recover a Deleted Commit?

Run git reflog to see all recent HEAD movements including deleted commits.

Find the commit hash of your lost commit, then run git reset --hard commit-hash to restore it.

Dangling commits remain recoverable for approximately 30 days before garbage collection removes them permanently.

Verification

Confirm your commit deletion worked with these checks:

  • Run git log --oneline locally to verify the commit is gone
  • Check GitHub web interface to confirm remote branch updated
  • Compare local and origin with git diff origin/branch-name
  • Run git status to ensure no unexpected staged or unstaged changes

Troubleshooting

Issue: Force Push Rejected by Remote

Solution: The remote has branch protection rules enabled.

Contact your repository admin to temporarily disable protection, or use a pull request workflow instead.

Issue: Commits Still Visible After Reset

Solution: You reset locally but did not push changes to remote.

Run git push origin branch-name --force to sync the remote with your local history.

Issue: Rebase Conflicts During Interactive Rebase

Solution: Git cannot automatically apply changes after removing the commit.

Fix conflicts in affected files, run git add ., then git rebase --continue. To abort, use git rebase --abort.

Learn more about resolving merge conflicts in Git.

Issue: Team Members Have Old Commits

Solution: Notify teammates before force pushing shared branches.

They need to run git fetch origin then git reset --hard origin/branch-name to sync with the rewritten history.

Related Processes

Now that you understand commit deletion, explore these connected workflows:

Understanding source control management fundamentals helps prevent situations where commit deletion becomes necessary.

Consider implementing a code review process to catch mistakes before they reach the main branch.

FAQ on How To Delete A Commit In Github

Can I delete a commit without losing my changes?

Yes. Use git reset --soft HEAD~1 to remove the commit while keeping all changes staged.

Your files stay intact. Only the commit record disappears from history, letting you recommit with modifications.

What happens if I delete a commit that was already pushed?

The commit remains on the remote until you force push.

Run git push origin branch-name --force after local deletion. Team members who pulled the old commit need to resync their local branches.

Is there a way to undo a commit deletion?

Yes. Run git reflog to find the deleted commit’s hash.

Then execute git reset --hard commit-hash to restore it. Works for approximately 30 days before garbage collection purges dangling commits.

What is the difference between git reset and git revert?

Git reset removes commits from history entirely. Git revert creates a new commit that undoes changes while preserving the original.

Use reset for local branches; revert for shared branches.

Can I delete a specific commit from the middle of my history?

Yes. Use interactive rebase with git rebase -i HEAD~N where N includes your target commit.

Change pick to drop next to the commit. Save and close the editor.

Will deleting commits affect other branches?

Only if those branches share the same commits. Deleting from one branch does not automatically affect others.

Merged commits may still exist in destination branches even after deletion from source.

How do I delete all unpushed commits at once?

Run git reset --hard origin/branch-name to match your local branch with remote.

All local commits not on the remote disappear. Your working directory reverts to the remote state.

Is it safe to force push to a shared repository?

Not without coordination. Force pushing rewrites history and breaks teammates’ local copies.

Use --force-with-lease instead for safety. Better yet, communicate with your team before any force push on shared branches.

Can I delete commits from a pull request?

Yes. Delete commits locally from your feature branch, then force push to update the pull request.

The PR automatically reflects your updated branch history. Reviewers see the cleaned-up commit list.

How do I prevent accidental commits that need deletion?

Use gitignore to exclude sensitive files. Review changes with git diff before committing.

Set up continuous integration checks to catch secrets and errors before they reach the main branch.

Conclusion

Learning how to delete a commit in GitHub gives you control over your repository history when mistakes happen.

Use git reset –soft to keep changes staged. Use git reset –hard when you want everything gone.

For commits already pushed to origin, force push updates the remote branch. The –force-with-lease flag adds safety on shared branches.

Interactive rebase handles surgical removals from the middle of your commit log output.

Always check git reflog` before panicking. Deleted commits stick around for about 30 days.

Master these commands and local repository cleanup becomes routine. Understanding Git workflow patterns helps you avoid needing them in the first place.

Practice on a test branch first. Then handle production with confidence.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Delete a Commit in GitHub
Related Posts