I need to permanently and completely remove a git repository's commit history and continue with the current version of the files. Older versions / commits must not be accessible in any way. I have tried creating a new master branch, removing all other branches, but old commits keep showing when I try 'git show xxxx' in git bash.
I have tried creating a new master branch, removing all other branches, and using git gc.
For such a complete clean up, you need to create a brand new repo and delete the old one.
I could give you answers about running garbage collection, and clearing the reflog, and finding the other places where Git stores deleted commits for a while, but with the question as you ask it, I strongly recommend a fresh repo.
Especially, if you are using GitHub or some other online Git server, full cleaning of old commits may be a hopeless task.
OK, so my answer, "give up, it cannot be done" is not very satisfactory. Here are some commands that might purge old commits from a sandbox:
The reflog keeps pointers to where HEAD pointed to for some time back. git gc will not delete any commit still pointed to by the reflog, because they're not actually loose objects if they're still pointed to by the reflog.
This worked for me:
git reflog expire --expire=all --all
Validation: run git reflog and make sure it's empty.
Any tags or branches still pointing into the old history will make sure that history cannot be deleted.
git tag -d <tagname>
git branch -D <oldbranchname>
If You still have origin/master pointing to a commit, it cannot be garbage collected. So either remove the old remote, or delete all the tags and references on the old remote too and prune their references in your sandbox:
git fetch --prune
or even
git remote remove origin
Validation: run git log --all and make sure the old commits are not listed.
Now, you can run garbage collection, with options to make it as thorough as possible.
git gc --prune=now --aggressive
At this stage, finally, the old master commit is no longer shown by git show <old-sha1-of-master> in my test repo.
This is where things get harder, because you have to know what types of reflog-like things and backups your server uses. But...
Once you've checked all of the above, try a fresh git clone and git clone --mirror. Also try loading this in your browser: https://<server>/<user>/<repo>/commit/<sha1>. If none of these show the commits you wanted removed, then I guess you're done?
Realistically, I don't think the above test will say you're done. Server side, if you really want to remove the old history with any secrets it might contain, I'm back to my unsatisfying initial answer: delete the repo from your Git server completely (accept all the warnings that say "this is not reversible" - that's what you want, after all!), and create a new repo with an empty history, an empty list of PRs, empty backups, and push to it just the history you want.
Update: this answer to a related question, Remove sensitive files and their commits from Git history says you can contact GitHub customer support to get a dangling commit with sensitive information actually deleted from your repo.
Once your sandbox and server are fixed, don't forget that:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With