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>...]'
13
  • Is this the first commit you are 'on' ? So reseting to HEAD^ is not possible because there is no commit to go to. Commented Nov 17, 2017 at 12:13
  • Close but you need a "~" rather than a "^": stackoverflow.com/questions/927358/… Commented Nov 17, 2017 at 12:13
  • Possible duplicate of How to undo the last commits in Git? Commented Nov 17, 2017 at 12:14
  • 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? Commented Nov 17, 2017 at 12:18
  • 1
    @johndoe, in the link I provided you have the answer. Delete .git folder or use update-ref. Commented Nov 17, 2017 at 12:29

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

eloquently explained :)
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

3 Comments

So there is no way to do that except deleting the git directory ??
did you just start this git repository? are there no other branches or commits?
@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
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

3 Comments

Remember that this will rewrite the history and if he already has a pushed history this will result in some major issues.
read question! He wants to rewrite history, there is no push yet this is first commit.
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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.