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.
Understanding the implications
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.
Step 1: Identify the commit
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.
Step 1a: Use the Graphite CLI visual log
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.
Step 2: Deleting a specific commit
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.
Start an Interactive Rebase:
Terminalgit rebase -i <commit-hash>^Replace
<commit-hash>
with the hash of the commit just before the one you wish to delete.In the editor that opens, find the line with the commit you want to delete, change
pick
todrop
, or simply delete the line.Save and close the editor to start the rebase.
Push your changes using
git push --force
once the rebase has completed.
Step 3: Deleting the last commit
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.
Step 4: Cleaning up remote history
If the commit has been pushed to a remote repository (like GitHub), and you need to update the remote history:
Force push the changes:
Terminalgit push --forceThis command updates the remote branch with your local branch, including all history changes. Use caution, as this will overwrite the remote history.
Step 5: Deleting all commits from a branch
To remove all commits from a specific branch:
Checkout the branch:
Terminalgit checkout the-branchDelete all commits:
Terminalgit reset --hard $(git commit-tree HEAD^{tree} -m "Initial commit")Force push if needed:
Terminalgit push origin the-branch --forceThis will wipe the remote branch of its entire commit history while retaining the branch itself.
Step 6: Deleting a branch completely
If you want to delete an entire branch, both locally and from GitHub or another remote:
Delete the branch locally:
Terminalgit branch -D the-branchDelete the branch from the remote:
Terminalgit push origin --delete the-branchThis 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.
Additional considerations
- 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.