Karush Logo

Clear Git History

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

When I use git (and github) for source control I do it more to have a backup of my code in a remote location than anything else. Having an extensive history of changes is interesting, but when working as an individual or even on a small team I've seldom wanted to roll back to anything but the previous version.

I have also sometimes inadvertently committed sensitive content such as password information that I've wanted to remove.

As such I needed a simple and clean way to get rid of the history in a git repository, so that all I have is my most current commit. This document will provide the steps for doing so.

Note: be careful when performing the following steps as git history will be permanently deleted.

Note: this information is taken from shellhacks.com, and is included in my blog for my documentation/reference purposes.

Clear Git History

In your code directory do the following:

Create a temporary branch and checkout:

git checkout --orphan temp_branch

Stage all files in the temporary branch and commit the changes:

git add * git commit -m "initial commit"

Delete the "main" branch (note it may be named master in older repositories):

git branch -D main

Rename the temporary branch to "main":

git branch -m main

Forcefully update the remote repository:

git push -f origin main

Verify

View your git history to see that you now have just one commit:

git log --oneline

Also view your files on github and note that all items are associated to "initial commit".

Conclusion

When working on individual projects or on a small team, or if you have inadvertently committed sensitive content such as password information, you might want to truncate your git history.