Reputation: 9680
I know this question has been asked thousands of time and I tried all solutions and none worked.
I committed to my local 2-3 files and now I want to undo the commit. How can I do that: This is also the first ever commit.
git reset HEAD^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
UPDATE:
git reset --soft HEAD~1
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
UPDATE 2:
git reset --hard HEAD~1
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Upvotes: 1
Views: 1058
Reputation: 45819
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
.)
Upvotes: 4
Reputation: 38161
Since this is first commit best approach is update your index to the state you what to achieve and then
git commit --amend
https://nathanhoad.net/git-amend-your-last-commit
Upvotes: 1
Reputation: 1573
if it's your first commit then there is nothing to revert to. there is no state that git knows before this commit.
Maybe you can edit the commit with https://www.atlassian.com/git/tutorials/rewriting-history
Otherwise you can delete the .git Folder in your Directory and start again with git init
Upvotes: 2