Git – Remove All Commits – Clear Git History (Local & Remote)

In this article i am showing how to clear Git history by removing all commits.

You may need this if you want to delete sensitive data from the history of Git commits.

After such cleanup you will have the latest version of your Git repository, but with the one commit only.

Be aware that after resetting all the history of changes in your Git repository will be deleted as well.

Cool Tip: Made a mistake? Undo last Git commit! Read more →

Clear Git History

Create a temporary branch and checkout:

$ git checkout --orphan temp_branch
Option Description
--orphan Create a branch in a git init-like state

Add all files to the temporary branch and commit the changes:

$ git add -A
$ git commit -am "The first commit"

Delete the master branch:

$ git branch -D master

Rename the temporary branch to master:

$ git branch -m master

Forcefully update the remote repository:

$ git push -f origin master

Cool Tip: Revert a file to the previous commit! Read more →

Was it useful? Share this post with the world!

25 Replies to “Git – Remove All Commits – Clear Git History (Local & Remote)”

  1. This was perfect. Thanks!

  2. Awesome! Thanks!

  3. Thanks, it works like a charm 🙂

  4. Beautiful, thanks so much

  5. Anvarbekov Jamshidbek says: Reply

    super

  6. Thanks this really helped

  7. Thank you. That worked perfect!

  8. Awesome…

  9. Perfect!

  10. Worked like a charm, thanks a lot !! 🙂

  11. Most excellent! Many thanks!

  12. Wow! This is a life saver!

  13. Lucky Jeremiah says: Reply

    Wow! This is the bomb! Many Thanksssss!

  14. Super helpful, thanks!

  15. David Jones MVP says: Reply

    Thanks, worked for me. Been looking for this for a while.

  16. Much much thanks!

  17. mara tata san says: Reply

    Amazing thank you!

  18. It works perfect with Codecommit! Thanks!

  19. Simple & efficient.

  20. Awesome!!

  21. Juan Carlos says: Reply

    error: refname refs/heads/temp_branch not found

  22. It works, but it still leaves the old versions of files in the .git directory

  23. Saved my day!

  24. skibididoopdiwoop says: Reply

    you saved my life

Leave a Reply