Introduction
We cannot reset to the initial commit using rebase
or revert
due to git’s security mechanism.
We cannot completely delete the .git
directory or we will lose everything in our project’s version control.
Here we are primarily concerned about the default branch, mostly main or master, as the first commit will land on it.
There are two ways to reset the commit history.
– Changing the main
branch
– Deleting the main
branch history
Changing the main
branch approach
- Rename
main
branch to something else. To a branch name that does not exist.
git branch -m main main_bkp
-m
(or --move
) renames the branch and moves the reflog.
- Create a orphan branch from the current head.
git checkout --orphan main
--orphan
creates a branch without any history or a parent. Totally disconnect branch from other branches/commits.
- Delete main branch backup
This is last step as we don’t need the backup branch anymore.
git branch -D old_main
We still have all the changeset and brand new main
branch with no commit history.
Deleting the main
branch history approach
Same can be achieved with single command with this approach.
git update-ref -d HEAD
This command is risky as there is no backup. But if you know what you are doing, then this will be most efficient method.