How to revert initial git commit?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

This question was linked from this blog post and an alternative solution was proposed for the newer versions of Git:

git branch -m master old_master
git checkout --orphan master
git branch -D old_master

This solution assumes that:

  1. You have only one commit on your master branch
  2. There is no branch called old_master so I'm free to use that name

It will rename the existing branch to old_master and create a new, orphaned, branch master (like it is created for new repositories) after which you can freely delete old_master... or not. Up to you.

Note: Moving or copying a git branch preserves its reflog (see this code) while deleting and then creating a new branch destroys it. Since you want to get back to the original state with no history you probably want to delete the branch, but others may want to consider this small note.

Solution 4 - Git

Under the conditions stipulated in the question:

  • The commit is the first commit in the repository.
  • Which means there have been very few commands executed:
    • a git init,
    • presumably some git add operations,
    • and a git commit,
    • and that's all!

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.

Solution 5 - Git

I will throw in what worked for me in the end. I needed to remove the initial commit on a repository as quarantined data had been misplaced, the commit had already been pushed.

Make sure you are are currently on the right branch.

git checkout master

git update-ref -d HEAD

git commit -m "Initial commit

git push -u origin master

This was able to resolve the problem.

Important

This was on an internal repository which was not publicly accessible, if your repository was publicly accessible please assume anything you need to revert has already been pulled down by someone else.

Solution 6 - Git

One option is to delete the repository and create a new one:

rm -rf .git
git init

Solution 7 - Git

I wonder why "amend" is not suggest and have been crossed out by @damkrat, as amend appears to me as just the right way to resolve the most efficiently the underlying problem of fixing the wrong commit since there is no purpose of having no initial commit. As some stressed out you should only modify "public" branch like master if no one has clone your repo...

git add <your different stuff>
git commit --amend --author="author name <author.name@email.com>"-m "new message"

Solution 8 - Git

For me:

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

First line was as suggested by CB Bailey (not sure why the second line was necessary, but git didn't uncommit the files until I did that - I could tell by running git status before and after the second line above)

Solution 9 - Git

git reset --hard make changes, then do

git add -A
git commit --amend --no-edit 

or

git add -A
git commit --amend -m "commit_message"

and then

git push origin master --force

--force will rewrite that commit you've reseted to in the first step.

Don't do this, because you're about to go against the whole idea of VCS systems and git in particular. The only good method is to create new and delete unneeded branch. See git help branch for info.

Solution 10 - Git

Technically, to revert the initial commit, you'd do:

git revert HEAD

But since that records a new commit that reverses the effects of the last commit, you might as well just make a new commit yourself that fixes the issues introduced by your initial commit. That way, you'd keep your initial commit (Ie, you'd keep your history unchanged.) which you might want to do (especially if, eg, you've already pushed to a shared repository). Otherwise, you might prefer to just start over. Ie, delete the repository and create a new one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChau Chee YangView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitfrazrasView Answer on Stackoverflow
Solution 3 - GitnonsensickleView Answer on Stackoverflow
Solution 4 - GitJonathan LefflerView Answer on Stackoverflow
Solution 5 - GitedwardView Answer on Stackoverflow
Solution 6 - Gitma11hew28View Answer on Stackoverflow
Solution 7 - GitRichardView Answer on Stackoverflow
Solution 8 - GitstevecView Answer on Stackoverflow
Solution 9 - GitdamkratView Answer on Stackoverflow
Solution 10 - Gitabdallah NofalView Answer on Stackoverflow