How do I delete a commit from a branch?

Learn how do i delete a commit from a branch? with practical examples, diagrams, and best practices. Covers git, commit, git-commit development techniques with visual explanations.

How to Delete a Commit from a Git Branch

Hero image for How do I delete a commit from a branch?

Learn various methods to safely and effectively remove unwanted commits from your Git history, understanding the implications of each approach.

Deleting a commit from a Git branch is a common task, but it requires careful consideration due to the immutable nature of Git history. The best method depends on whether the commit has been pushed to a remote repository and if you want to preserve the changes introduced by the commit. This guide will walk you through the most common scenarios and their corresponding solutions.

Understanding Git History Manipulation

Before diving into specific commands, it's crucial to understand the implications of altering Git history. Git is designed to track changes, and each commit is a snapshot of your repository at a given time. When you 'delete' a commit, you're essentially rewriting history. This can lead to significant problems if the changes have already been shared with others, as it can cause divergence and force others to rebase or reset their local branches.

flowchart TD
    A[Start]
    A --> B{Commit to delete is local only?}
    B -->|Yes| C[Use git reset --hard]
    B -->|No| D{Want to keep changes?}
    D -->|Yes| E[Use git revert]
    D -->|No| F[Use git rebase -i (carefully!)]
    C --> G[End]
    E --> G
    F --> G

Decision flow for deleting a Git commit

Scenario 1: Deleting a Local-Only Commit (Not Pushed)

If the commit you wish to delete has not yet been pushed to a remote repository, you have more flexibility. The git reset command is the most straightforward way to undo local commits. There are a few variations of git reset, each with different effects on your working directory and staging area.

1. Identify the target commit

First, use git log to find the hash of the commit before the one you want to delete. This is the commit you want to reset to.

2. Perform a hard reset

To completely remove the commit and discard its changes from your working directory and staging area, use git reset --hard <commit_hash>. Replace <commit_hash> with the hash of the commit you want to revert to (the parent of the commit you want to delete).

3. Verify the change

After the reset, use git log again to confirm that the unwanted commit is no longer in your branch's history.

git log --oneline
# Example output:
# a1b2c3d Commit D
# e4f5g6h Commit C (This is the commit to delete)
# i7j8k9l Commit B (Reset to this commit)
# m0n1o2p Commit A

git reset --hard i7j8k9l

git log --oneline
# Expected output:
# i7j8k9l Commit B
# m0n1o2p Commit A

Using git reset --hard to delete a local commit

Scenario 2: Deleting a Pushed Commit (Rewriting History)

If the commit has already been pushed to a remote repository, deleting it involves rewriting history, which can be disruptive. This method should only be used if you are absolutely sure no one else has pulled your changes, or if you have coordinated with your team.

1. Reset locally

First, perform a local git reset --hard as described in Scenario 1 to remove the commit from your local branch.

2. Force push to remote

To update the remote branch with your rewritten history, you must use git push --force-with-lease origin <branch_name> or git push --force origin <branch_name>. The --force-with-lease option is generally safer as it prevents overwriting changes that someone else might have pushed in the meantime.

git reset --hard i7j8k9l # Reset to the commit before the one you want to delete
git push --force-with-lease origin my-feature-branch

Force pushing after a local reset

Scenario 3: Undoing a Pushed Commit (Preserving History)

When a commit has been pushed and potentially shared, the safest way to 'delete' it is by using git revert. This command creates a new commit that undoes the changes introduced by the specified commit, effectively canceling its effects without rewriting history. This is the recommended approach for public branches.

1. Identify the commit to revert

Use git log to find the hash of the specific commit you want to undo.

2. Revert the commit

Execute git revert <commit_hash>. Git will create a new commit that reverses the changes. You will be prompted to edit the commit message; save and close the editor to complete the revert.

3. Push the revert commit

Push the newly created revert commit to your remote repository using git push origin <branch_name>.

git log --oneline
# Example output:
# a1b2c3d Commit D
# e4f5g6h Commit C (This is the commit to revert)
# i7j8k9l Commit B

git revert e4f5g6h
# Git will open an editor for the commit message. Save and close.

git log --oneline
# Expected output:
# f0o1b2a Revert "Commit C"
# a1b2c3d Commit D
# e4f5g6h Commit C
# i7j8k9l Commit B

git push origin my-feature-branch

Using git revert to undo a pushed commit