Deleting Your Commit History

Moyuri Akther
2 min readDec 25, 2023

Step-1: Check out to a temporary branch

Since your main branch will serve as the foundation for your new history, make sure it is in the desired state first. Next, do

git checkout — tempo_branch orphan

With this command, a new branch called temp_branch is created, and it is switched to. When a branch is created with the — orphan option, it has no commit history.

Step-2: Add all files to the new branch

git add -A
All modifications, including deleted files, will be staged in the working directory as a result.

Step-3: Now First Commit to the new branch

With the following command, we can now commit the modifications to the orphan branch:
Commit git -m “First commit”
As a result, all project files are included in a single commit.

Step 4: Delete the main branch

Having successfully pushed the modifications to the new branch, we can now remove the local main branch that held our whole commit history. use this command -
git branch -D main

Step 5: Rename the temporary branch to the main branch

The temporary branch must then be renamed to the main branch:
git branch -m main

Step 6: Force update to our repository

Finally, we must use the following command to forcefully update our repository: Utilizing

git push — force origin main
We can replace our new local history with the remote main branch’s history by using the — force option. Generally speaking, you should utilize this option carefully as you run the danger of losing previous work that you or others have completed. But for us, this is the desired outcome.

--

--