90% of the relevant info is already here in other answers, but I think a little clarification is in order:
The things you've tried, that you think don't work, aren't working only because of the special case that this is the only commit in the repo. For example
git reset HEAD^
means to take the index and current branch (assuming there is a current branch) back to the previous commit. But there is no previous commit, so git doesn't know what you mean.
There are still several ways to undo the commit. Several people have suggested that deleting the .git
directory and rerunning git init
might be the simplest. This assumes there's nothing of value in the .git
directory. Certainly the database is empty (except for the commit you don't want); but maybe there's something you care about in the index; or maybe you have something set up with hooks; or you don't want to re-do configuration, remotes, etc. Whatever. There may be reasons why deleting and re-init
ing isn't the solution for you.
The next suggestion, then, would be to use
git commit --amend
for your next commit. This will tell git that instead of making a new commit with the current HEAD
commit as its parent, it should replace the current HEAD
commit with the new commit. The new commit will get the same parent as the current HEAD
commit had (which in this case is "none"). The current branch ref will move to the new commit, effectively removing the current HEAD
commit from history.
But maybe for some reason you really want to fix this now instead of waiting for the next commit. Honestly there's not much reason, but for the sake of argument... Well, you can do that. It might be something like
git checkout --detach
git branch -d master
git checkout --orphan master
What we're doing here is going into "detached HEAD
" state so that we don't have master
checked out. This allows us to delete master
, and then recreate it as an "orphan" branch - i.e. one with no history. This again knocks the bad commit out of the history.
It may be worth noting that none of these techniques will immediately delete the old commit; but they do ensure that it won't be included in any push, or default log
output, etc. The commit will eventually go away, once it rolls off the reflog and the garbage collector catches up. If you don't want to wait, there are steps you can take to immediately delete the commit. (Since your repo is basically empty, one option there - again the simplest unless it has negative side effects - would be to delete the .git directory and re-init
.)