1

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>...]'
Marek R
  • 32,568
  • 6
  • 55
  • 140
john doe
  • 9,220
  • 23
  • 91
  • 167
  • Is this the first commit you are 'on' ? So reseting to HEAD^ is not possible because there is no commit to go to. – ckruczek Nov 17 '17 at 12:13
  • Close but you need a "~" rather than a "^": https://stackoverflow.com/questions/927358/how-to-undo-the-last-commits-in-git – Edwin Nov 17 '17 at 12:13
  • Possible duplicate of [How to undo the last commits in Git?](https://stackoverflow.com/questions/927358/how-to-undo-the-last-commits-in-git) – iBug Nov 17 '17 at 12:14
  • @iBug Yeah, I mentioned that literally on the first line even the first sentence of the question :) – john doe Nov 17 '17 at 12:15
  • 1
    @SketchyCoder: Not quite corret. Both are valid but have different meanings in different context. So my initial question is still unanswered: Is there only one commit and is it the first one? – ckruczek Nov 17 '17 at 12:18
  • @johndoe is that it: https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit ? – Opal Nov 17 '17 at 12:18
  • Yes this is the first ever commit to this project – john doe Nov 17 '17 at 12:21
  • Just use `git revert` and revert the commit and you are in your initial state. – ckruczek Nov 17 '17 at 12:26
  • 1
    @johndoe, in the link I provided you have the answer. Delete `.git` folder or use update-ref. – Opal Nov 17 '17 at 12:29
  • @ckruczek he can't revert further than the first commit, which is where he is – Chris Stadler Nov 17 '17 at 12:29
  • @RodrigoStadler; Ofcourse he can use `git revert` this will create a new commit with the inverse-changes of his first commit. I don't see a problem with this. – ckruczek Nov 17 '17 at 12:32
  • @ckruczek the problem is that it reverts to the last commit. it doesn't revert to what you did before you committed. I also tested this locally so I can guarantee this does not work – Chris Stadler Nov 17 '17 at 14:24
  • git revert does not work. git revert usage: git revert [] ... or: git revert – john doe Nov 17 '17 at 14:25

3 Answers3

4

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-initing 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.)

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
2

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

Chris Stadler
  • 1,563
  • 1
  • 15
  • 17
  • So there is no way to do that except deleting the git directory ?? – john doe Nov 17 '17 at 12:22
  • did you just start this git repository? are there no other branches or commits? – Chris Stadler Nov 17 '17 at 12:23
  • @johndoe then yes, if there is nothing else on there it shouldn't be a problem. that way you can start fresh. your files won't be deleted and since you don't have any existing commits or branches it won't be a problem – Chris Stadler Nov 17 '17 at 12:26
1

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

Chris Stadler
  • 1,563
  • 1
  • 15
  • 17
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Remember that this will rewrite the history and if he already has a pushed history this will result in some major issues. – ckruczek Nov 17 '17 at 12:33
  • read question! He wants to rewrite history, there is no push yet this is first commit. – Marek R Nov 17 '17 at 12:34
  • I know. This was just a quick reminder for others if they are reading your answer. I just posted it as a reminder, no criticism on your answer ;) – ckruczek Nov 17 '17 at 12:36