As you work with Git, your commit history can become cluttered with unnecessary or sensitive information. 

You may need to delete your commit history to remove accidentally committed sensitive data, start fresh, or reduce your repository size for better performance. 

In this guide, you’ll learn how to delete all commit history in Git, step by step. 

We’ll walk you through the process, from understanding the consequences to verifying the result, with command examples to help you along the way.

Backing Up the Repository

Backing up your repository is an important step in the deletion process. 

This ensures that you can recover your data in case something goes wrong during the deletion process. 

Take a few minutes to create a backup of your repository using git clone or other methods to safeguard your data.

Ensuring all local changes are committed or stashed

Make sure you commit or stash all local changes before deleting your commit history. 

Use git status to check for any uncommitted changes and git stash to temporarily store them.

Stashed changes can be reapplied later using git stash apply, ensuring that you don’t lose any important work. 

Take the time to review your changes and commit or stash them accordingly.

Step-by-Step Guide to Deleting Commit History

Suppose your repository has only 1 branch with name main that has multiple commits and you want to remove all commit history.

Following below steps will enable you to delete all history and have only 1 commit.

1. Switch to a temporary branch

Deleting your commit history starts with switching to a temporary branch. Run the command 

git checkout --orphan TEMP_BRANCH

to create a new branch that doesn’t have any commit history.

2. Create a new, clean commit with the desired files

Add the files that you need to commit with 

git add .

and commit the changes with

git commit -m "Initial clean commit".

3. Delete the main branch

So, all your changes have been committed into the new branch in Step 1 and 2, now delete the main branch with command

git branch -D main

This will delete main branch from your local and remote as well.

4. Rename the current branch

Now you are left with only 1 branch that we created in Step 1.
Rename this branch to main using command

git branch -m main

5. Forcing the main branch to the new, clean commit

Finally, commit this main branch to the repository with below command

git push -f origin main

-f stands for force push.

If all the steps were success, you will see only 1 commit in the history, which will be the commit that we did in Step 2.

Advantages of deleting commit history

Any developer who has accidentally committed sensitive information knows how stressful it can be. 

Deleting commit history provides a way to remove this information and ensure your repository is secure.

Disadvantages of deleting commit history

For developers who rely on commit history to understand the project’s evolution, deleting it can be a significant loss. 

You will no longer be able to track changes, identify errors, or understand the context behind certain decisions.

For instance, imagine trying to debug an issue without being able to see the commit history. 

You might struggle to identify the cause of the problem, making it much harder to fix. 

This highlights the importance of carefully considering the implications of deleting commit history before taking the leap.

Conclusion

On the whole, you have now successfully deleted your entire commit history in Git. 

By following the step-by-step guide, you have created a new, clean commit and forced it to become the new main branch. 

Always exercise caution when deleting commit history, and consider alternative methods if possible.