How to delete a commit from Git history

Greg Foster
Greg Foster
Graphite software engineer

In some scenarios, such as when a major mistake needs correcting, you might need to delete a commit from your Git history. This guide explores various methods to remove commits from a branch's history, both locally and from remote repositories.

Note: While the information in this guide discusses how to delete commit history, after data has been pushed to Git it can still exist elsewhere such as on local repositories that have already been cloned. Once sensitive data has been committed to your repo you should immediately consider it compromised.

For deleting sensitive information such as user data or credentials, see our guide on deleting sensitive data from Git.

Before proceeding with deleting commit history in Git, it's crucial to understand the implications:

  • Loss of data: Once a commit is deleted, the changes it contained should be considered permanently lost unless backed up elsewhere.
  • Impact on collaboration: If you're working in a team and the branch is shared, deleting commits can disrupt the workflow for others who might have based their work on this history.

First, identify the commit you want to delete. You can view the commit history with:

git log

Take note of the commit hash (a unique identifier generated by hashing the content of the commit along with its metadata using the SHA-1 hash function) of the commit you want to delete.

To remove a specific commit from your history, you can use git rebase. This method is preferable for commits that are not at the head of the branch.

  1. Start an Interactive Rebase:

    git rebase -i <commit-hash>^

    Replace <commit-hash> with the hash of the commit just before the one you wish to delete.

  2. In the editor that opens, find the line with the commit you want to delete, change pick to drop, or simply delete the line.

  3. Save and close the editor to start the rebase.

  4. Push your changes using git push --force once the rebase has completed.

If the commit you want to delete is the most recent one:

git reset --hard HEAD^

This command will remove the most recent commit from your local branch.

If the commit has been pushed to a remote repository (like GitHub), and you need to update the remote history:

  1. Force push the changes:

    git push --force

    This command updates the remote branch with your local branch, including all history changes. Use caution, as this will overwrite the remote history.

To remove all commits from a specific branch:

  1. Checkout the branch:

    git checkout the-branch
  2. Delete all commits:

    git reset --hard $(git commit-tree HEAD^{tree} -m "Initial commit")
  3. Force push if needed:

    git push origin the-branch --force

    This will wipe the remote branch of its entire commit history while retaining the branch itself.

If you want to delete an entire branch, both locally and from GitHub or another remote:

  1. Delete the branch locally:

    git branch -D the-branch
  2. Delete the branch from the remote:

    git push origin --delete the-branch

    This will delete the branch from the repository, however will not immediately delete it's commit history. The commits themselves will remain in the repository's history if they are accessible through other branches or tags. If the deleted branch contained unique commits (not merged or referenced elsewhere), those commits will become "dangling" and will eventually be garbage collected by Git, making recovery more difficult but not immediately erasing them.

  • Backup before deletion: Consider backing up your data before performing deletions, especially in a collaborative environment.
  • Communication: Inform your team about the history change, as they will need to rebase any local changes they have based on the old history.
  • Avoid frequent history rewrites: Regularly rewriting public history can undermine the integrity and reliability of your repository. Reserve these actions for necessary situations.

For more information on removing data from your Git repository see the official GitHub documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2