Delete entire commit history of a git repository
You are working on a repository which already has lots of commits, and you come across a situation that you need to delete the entire commit history and want to start fresh and make the Initial Commit, then follow along with this article.
You can safely carry out the following steps:
- First, commit all your changes to the main branch if you have any.
git commit -m “message”
2. Now, create a new branch using the orphan switch
git checkout --orphan new-branch
You will be switched to new-branch. Confirm the same by running git status. The first line will report you if you are in the right branch.
Some info about “orphan” switch
— orphan
Creates a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.
Above highlighted line in bold will do the trick.
Please follow this link for more information.
Now, if you run git status command, you will see that all the files are in the new file state.
3. Cool, now let’s add all the files
The option “-A” or “ — all” is used to add all new and updated files everywhere throughout the project, to the staging area
git add -A
or
git add --all
4. Commit the changes now
“-am” switch is a combination of -a & -m. “-a” is used to add the modified files in the staging area and “-m” is used for providing the comment.
git commit -am “Initial Commit”
or simply
git commit -m “Initial Commit”
5. Delete the master branch, which has all the commit history now
git branch -D master
6. Rename the new-branch to master
git branch -m master
7. We have reached the final step now. You will have to force update the repository
git push -f origin master
That’s it !!
Now if you go to GitHub, you will only see the “Initial Commit” in history.
Thanks for reading!
Please hit the applaud button and share, to recommend this article if you find it helpful.