It is generally not a good idea to delete commit history in a Git repository, as the commits represent the history of the project and the changes that have been made to it. However, there may be situations where you want to delete commits, either because they contain sensitive information or because you want to reorganize the history of the repository.

Advertisement

Delete Commit History in Github Repository

Follow the below instruction to completely delete the commit history of the GitHub repository.

Warning: This will remove your old commit history completely, You can’t recover it again.
  1. Create Orphan Branch – Create a new orphan branch in git repository. The newly created branch will not show in `git branch` command.
    git checkout --orphan temp_branch 
    
  2. Add Files to Branch – Now add all files to the newly created branch and commit them using the following commands.
    git add -A 
    git commit -am "the first commit" 
    
  3. Delete master Branch – Now you can delete the master branch from your git repository.
    git branch -D master 
    
  4. Rename Current Branch – After deleting the master branch, let’s rename the newly created branch name to master.
    git branch -m master 
    
  5. Push Changes – You have completed the changes to your local git repository. Finally, push your changes to the remote (Github) repository forcefully.
    git push -f origin master 
    

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

In this tutorial, we have described to you to remove the commit history of a GitHub repository. The same instruction can be followed on other upstream Git repository providers like Gitlab, BitBucket, etc.

Share.

13 Comments

  1. Felipe Sodre on

    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.

  2. 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…

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

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

Leave A Reply