How to hide commit history on GitHub?

Ever pushed something on GitHub that you shouldn’t have pushed? Or ever wanted to remove old commits from your Github repository? Well, this is exactly what we’re going to do in this article, we’re going to talk about how you can hide commit history in the easiest way. So, without further talking, let’s get started.

person coding in laptop
Photo by Lukas on Pexels.com

All our commits are saved under a git branch. And all our git branches are saved under the .git folder. So we can either delete the particular branch safely or the .git folder. But deleting the git folder may not be the best option, because it may cause problems in our repository. But let’s try both methods anyway. Don’t worry, our code in the current state will remain the same.

Hide commit history on Git Branch

The whole concept is to create a new branch, move all current files to the newly created branch, delete the old branch and make the new branch the default one. Here’s how to do it.

First, create a new temporary branch and checkout.

git checkout --orphan TEMP_BRANCH

Now, add all the current files to this temporary branch.

git add -A

Create a new commit.

git commit -am "Initial commit"

Delete the old branch. In my case, my old branch name is master. Check what’s yours and modify if necessary

git branch -D master

Rename the temporary branch to your old branch name. Once again, my old branch was master. So, you should double-check our old branch name before deleting and renaming.

git branch -m master

Finally, force push the code to the GitHub branch

git push -f origin master

This should work perfectly. You can now visit your GitHub repository to check if there’s any remaining history available. But if it doesn’t work for some reason, the next method is definitely gonna help you.

Delete old commits using .git folder

This concept is to delete the .git folder so all our commit history goes along with it. Then initial a new git repo and force push it to our existing repository on GitHub. Here are the steps:

First, clone your repository. Make sure you have changed the project URL. Here I’m using an example project from my GitHub Profile.

git clone https://github/khokonm/example.git

Now, navigate to your newly cloned project.

cd example

Delete the .git folder to delete all previous commits with it.

git rm -rf .git

Now initial a new repository on the current folder and add your remote git URL. I’m using my example project again.

git init
git remote add origin https://github.com/khokonm/example.git
git remote -v

Add all the files.

git add --all

Create a new commit.

git commit -am "Initial commit"

Finally, force push the new repository to your old one on GitHub.

That’s it! I hope you found this article helpful. If you face any problems, let me know in the comment section. You can read my other articles from here. This article was inspired by Heiswayi.

Hi! I write ad free content so you can read without any interruption. If you like my content, please consider buying a coffee for me.
Thanks for your support!

Support me on Buy me a coffee! Buy me a coffee!

Khokon M.

Full Stack Web Developer, Content Writer.

Leave a Reply