Removing Git History

Here’s a step-by-step guide to removing all Git commit history while retaining the current state of your files. This will also remove all branches and tags. For these instructions, I use Visual Studio Code, but this can be done all from Git Bash.

Because my GitHub is public and often traversed, I want to remove all commit history and keep things clean.

Preliminary Steps:

  1. Backup Your Repository: Make sure to backup your repository before proceeding, as these steps are irreversible.
  2. Inform Team Members: If you’re working in a team, inform them about this operation since it will rewrite the repository’s history.

Clone the Repository to Visual Studio Code:

  1. Navigate to GitHub and find the repository you wish to clone.
  2. Clone the repository to Visual Studio Code.

Remove All Branches and Tags:

  • You can do this via GitHub or another Git client. This is crucial if you want to wipe the entire Git history.

Execute Commands in Visual Studio Code Terminal:

Execute the following commands in the terminal:

Create a New Orphan Branch. Replace “new_branch” with your desired branch name.

git checkout --orphan new_branch

Stage All Changes.

git add -A

Commit Changes.

git commit -am "Fresh initialization"

Delete Main Branch. Make sure you are not on the “main” branch when you run this command.

git branch -D main

Rename Current Branch to Main.

git branch -m main

Force Push to Remote Main Branch. This will overwrite all history on the remote “main” branch.

git push -f origin main

Cleanup and Optimize Local Repository.

git gc --aggressive --prune=all

That’s it! You’ve now removed all the Git commit history and started fresh. Remember that this is a destructive and irreversible action once pushed to a shared repository, so proceed cautiously.

Leave a Reply