Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert initial git commit?

Tags:

git

I commit to a git repository for the 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 the first commit of the repository. Any idea how to undo git's initial commit?

like image 920
Chau Chee Yang Avatar asked Jul 09 '11 01:07

Chau Chee Yang


People also ask

How do I un initialize git?

While there is no undo git init command, you can undo its effects by removing the . git/ folder from a project. You should only do this if you are confident in erasing the history of your repository that you have on your local machine.


2 Answers

You can delete the HEAD and restore your repository to a new state, where you can create a new initial commit:

git update-ref -d HEAD 

After you create a new commit, if you have already pushed to remote, you will need to force it to the remote in order to overwrite the previous initial commit:

git push --force origin 
like image 25
frazras Avatar answered Oct 02 '22 21:10

frazras


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.

like image 95
CB Bailey Avatar answered Oct 02 '22 21:10

CB Bailey