GitHub is an essential tool for developers and teams to manage, collaborate, and track their work. However, sometimes you might need to delete a commit history, either to clean up the repository or remove sensitive information. In this article, we’ll walk you through the process of deleting commit history from GitHub using Git commands
Delete Commit History from Github Repository
Follow the below instruction to completely delete the commit history of the GitHub repository.
- Backup your Repository: Before making any changes, it’s crucial to backup your repository. You can do this by cloning the repository to a separate directory on your local machine:
git clone https://github.com/username/repo-name.git backup-repo
Replace “username” with your GitHub username and “repo-name” with the name of the repository you want to backup.
- Create a Orphan Branch: Create and checkout a new branch that will serve as the starting point for your cleaned-up history. It’s a good practice to use a descriptive name, such as “cleaned-history”:
git checkout --orphan cleaned-history
- Add and Commit Changes: Now, add all your files to the new branch and commit the changes. This will create a new commit with the current state of your files, but without the previous commit history:
git add -A
git commit -am "the first commit"
- Delete Old Branch: Delete the old branch that contains the history you want to remove. If your main branch is named “main”, you can delete it with the following command:
git branch -D main
- Rename the New Branch: Rename the “cleaned-history” branch to “main” or the name of the original branch you deleted:
git branch -m main
- Force Push the Changes to GitHub: Now you need to push the changes to GitHub, overwriting the remote branch. This action will permanently delete the commit history from the remote repository:
git push -f origin main
Keep in mind that deleting commit history is a destructive operation, as it permanently removes commits from the repository. It is generally not recommended to delete commit history unless it is absolutely necessary.
Conclusion
By following these steps, you’ve successfully deleted the commit history from your GitHub repository. Remember, this action is irreversible, so always backup your repository before making any changes. It’s essential to understand the implications of these actions to avoid potential issues in your development workflow.
14 Comments
thanks for info
Exactly what I needed!
Thank you!
Dear RAHUL, I have a branch on upstream that has already a open Pull Request with some comments.
What happens to my PR if I execute this procedure you suggest. Does my PR get closed?
Is there any other way that I can just remove my commits comments history, keeping just the latest one on my java files.
I had to commit several versions as a test. I don’t want the next developer get the file with several comments as testing.
Thanks.
It’s helped, thanks!
They say: “… please clear history and try again…”
But they didn’t make any way to clear that from UI, what is the point…
Great! Thanks 🙂
Salute!
Thank you!
i love you. i couldnt remove sensitive data from the repo because i burried it with more commits and nothing else worked until i found this <3
Thanks, this helped. However, the remote server still keeps the old commits and they may be accessible, e.g. from web GUI or by commit hash. This is a problem if you accidentally commited a password.
The fix is to ssh into the bare repo and run `git gc –prune=now`. That should drop them.
Works like a charm. Thank you!
Thanks!
Excellent.
Thanks
Thanks.