Chau Chee Yang Chau Chee Yang - 2 months ago 19
Git Question

How to revert initial git commit?

I commit a git repository at first time, I then regret the commit and want to revert it. I try

# git reset --hard HEAD~1


I get this message:

fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.


This commit is first commit of repository, any idea how to undo git initial commit?

Answer

You just need to delete the branch you are on. You can't use git branch -D as this has a safety check against doing this. You can use update-ref to do this.

git update-ref -d HEAD

Do not use rm -rf .git or anything like this as this will completely wipe your entire repository including all other branches as well as the branch that you are trying to reset.