How do I delete a commit from a branch?

To delete a commit from a Git branch, the method depends on whether the commit is local (unpushed) or already pushed to a remote repository. Here are the most common approaches:

1. Delete the Latest Commit (Local)

If the commit is the most recent one and not yet pushed, use git reset to remove it:

# Discard the last commit and its changes (careful: this deletes your work!)
git reset --hard HEAD~1

# Keep changes in your working directory (undoes commit but leaves files intact)
git reset --soft HEAD~1

# Keep changes in the staging area (commit undone, files staged)
git reset --mixed HEAD~1
  • HEAD~1: Refers to the commit before the current HEAD.
  • --hard: Deletes the commit and discards all changes.
  • --soft: Keeps changes in your working directory.
  • --mixed (default): Unstages changes but keeps them in the working tree.

2. Delete an Older Commit (Local)

Use interactive rebase to remove a specific commit from history:

# Rebase the last N commits (replace 3 with the number of commits to review)
git rebase -i HEAD~3

In the interactive editor:

  • Delete the line for the commit you want to remove, or replace pick with drop.
  • Save and exit. Git will rewrite history, excluding the deleted commit.

3. Delete a Pushed Commit (Remote)

If the commit is already on the remote (e.g., GitHub), you must force-push after rewriting history locally:

# Step 1: Remove the commit locally (use git reset or rebase)
git reset --hard HEAD~1

# Step 2: Force-push to overwrite remote history
git push origin your-branch --force

# Safer alternative for shared branches (avoids disrupting others):
git push origin your-branch --force-with-lease

⚠️ Warning: Force-pushing rewrites history and can break workflows for collaborators. Use cautiously!

4. Revert a Commit (Safe for Public Branches)

Instead of deleting the commit, create a new commit that undoes its changes (safe for shared branches):

# Revert a specific commit (identified by its hash)
git revert <commit-hash>

# Example:
git revert 1a2b3c4d

This preserves history while undoing the changes, making it ideal for collaborative workflows.

Example Scenarios

Scenario 1: Undo the latest local commit

git reset --hard HEAD~1

Scenario 2: Remove a commit from the middle of history

git rebase -i HEAD~5      # Target the commit 5 steps back
# Delete/drop the unwanted commit line in the editor
git push origin main --force

Scenario 3: Revert a pushed commit

git revert 1a2b3c4d       # Create a reversal commit
git push origin main      # No force needed

Key Considerations

  • Local vs. Remote: Use git reset or rebase for local commits, git revert for public ones.
  • Collaboration: Avoid force-pushing on shared branches unless your team expects it.
  • Recovery: Deleted commits can often be recovered using git reflog (within 30 days by default).

When to Use Each Method

MethodUse Case
git resetDiscard recent local commits (not yet pushed).
git rebase -iDelete/modify older commits (requires force-push if already pushed).
git revertSafely undo a public commit without rewriting history.

Let me know if you need further clarification!

Leave a Reply

Your email address will not be published. Required fields are marked *