#TIL 16 - Revert to 1st commit with Git command | ProductsWay

#TIL 16 - Revert to 1st commit with Git command

Jul 23, 2021 · Dung Huynh

What

Delete the current branch history while keeping every working-tree file, then clear the index so the files are unstaged.

Why

git update-ref -d HEAD deletes the branch pointer without touching your working tree or index. The old tracked files therefore appear as staged additions on the unborn branch. Remove them from the index to leave the files untracked, then start a fresh history. The old commits remain recoverable through git reflog until Git prunes them.

How

git update-ref -d HEAD
git rm --cached -r .

This rewrites local history. Back up the repository first, and do not use it on a shared branch. Recover the previous commit ID with git reflog if needed.