Deleting all commit history in Git

To delete all commit history, you can simple delete .git folder. But if you still want to keep the repository, there is a safe and easy method. The idea is using a new orphan branch to replace the existing branch. The new orphan branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

Delete all commit history

Follow below steps, assuming that you are on branch master:

# Step 1. Create a new orphan branch and swith to it.
# --orphan, create a new orphan branch and swith to it.
$ git checkout --orphan new-master

# Step 2. Delete the old branch
$ git branch -D master

# Step 3. Rename the new orphan branch to the old branch
$ git branch -m master

Now you have a new history that has no commits yet, you can do your first commit at the moment.

If you want to delete all the related commit objects immediately, run git gc to do an immediate garbage collection to remove them.

Resources

Leave a Reply