How to delete a commit from a branch

Posted on

Deleting a commit from a branch in Git can be achieved using several methods depending on whether the commit is the latest one or an older one, and whether you want to keep the changes or discard them entirely. The most common approach involves using git rebase or git reset to remove the commit from the branch’s history. These commands can rewrite commit history, so it’s important to use them with caution, especially when working with shared repositories.

Using git reset

Deleting the Latest Commit:
If the commit you want to delete is the most recent one, use git reset:

# Soft reset (keeps changes in working directory)
git reset --soft HEAD~1

# Hard reset (discards changes)
git reset --hard HEAD~1
  • HEAD~1: Refers to the commit before the most recent one.
  • --soft: Keeps changes in the working directory.
  • --hard: Discards changes entirely.

Example:

git reset --soft HEAD~1
  • This removes the latest commit but keeps the changes staged.

Using git rebase -i

Interactive Rebase:
For deleting an older commit, use interactive rebase:

git rebase -i HEAD~N
  • N: Number of commits to include in the interactive rebase.

Process:

  1. Start interactive rebase:
    git rebase -i HEAD~3
  2. In the editor, change pick to drop for the commit you want to delete.
  3. Save and close the editor.

Example:

pick abc1234 First commit
drop def5678 Commit to delete
pick ghi9101 Another commit
  • This removes the specified commit from the branch’s history.

Using git revert

Reverting a Commit:
If you want to undo a commit without altering history, use git revert:

git revert 
  • “: The hash of the commit to revert.

Example:

git revert def5678
  • This creates a new commit that undoes the changes made by def5678.

Using git cherry-pick

Selective Commit Application:
To remove a commit and reapply others, use git cherry-pick:

  1. Create a new branch from a previous commit:
    git checkout -b new-branch 
  2. Cherry-pick desired commits:
    git cherry-pick abc1234 ghi9101
  3. Replace the original branch:
    git checkout original-branch
    git reset --hard new-branch

Example:

git checkout -b temp-branch abc1234
git cherry-pick ghi9101
git checkout master
git reset --hard temp-branch
  • This sequence re-applies selected commits to a new branch and then hard resets the original branch.

Precautions and Best Practices

Backup:
Always create a backup branch before rewriting history:

git branch backup-branch
  • This safeguards your commits in case something goes wrong.

Force Push:
When working with remote repositories, a force push may be necessary:

git push origin branch-name --force
  • --force: Required to overwrite the remote history.

Coordination:
Communicate with your team before rewriting shared history to avoid conflicts and confusion.

Summary

Deleting a commit from a branch in Git can be done through various methods like git reset, git rebase -i, git revert, and git cherry-pick. Each method has its specific use case and impacts the commit history differently. Using git reset is suitable for the latest commit, while git rebase -i is more versatile for older commits. git revert is safe for undoing changes without altering history, and git cherry-pick is useful for selectively reapplying commits. Always back up your branch and communicate with your team when working on shared repositories to avoid potential issues.

Was this helpful?

Thanks for your feedback!