How to delete a commit from Git history

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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:

Terminal
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.

While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI

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:

    Terminal
    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.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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

Terminal
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:

    Terminal
    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:

    Terminal
    git checkout the-branch
  2. Delete all commits:

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

    Terminal
    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:

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

    Terminal
    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.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  • 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.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2