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 thi
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
You can't. So:
rm -rf .git/
git init
git add -A
git commit -m 'Your new commit message'
Under the conditions stipulated in the question:
git init
,git add
operations,git commit
,If those preconditions are met, then the simplest way to undo the initial commit would be:
rm -fr .git
from the directory where you did git init
. You can then redo the git init
to recreate the Git repository, and redo the additions with whatever changes are sensible that you regretted not making the first time, and redo the initial commit.
DANGER! This removes the Git repository directory.
It removes the Git repository directory permanently and irrecoverably, unless you've got backups somewhere. Under the preconditions, you've nothing you want to keep in the repository, so you're not losing anything. All the files you added are still available in the working directories, assuming you have not modified them yet and have not deleted them, etc. However, doing this is safe only if you have nothing else in your repository at all. Under the circumstances described in the question 'commit repository first time — then regret it', it is safe. Very often, though, it is not safe.
It's also safe to do this to remove an unwanted cloned repository; it does no damage to the repository that it was cloned from. It throws away anything you've done in your copy, but doesn't affect the original repository otherwise.
Be careful, but it is safe and effective when the preconditions are met.
If you've done other things with your repository that you want preserved, then this is not the appropriate technique — your repository no longer meets the preconditions for this to be appropriate.