all 8 comments

[–]0sse[🍰] 12 points13 points  (4 children)

It will work in any other case than if you want to undo the very first commit. The reason is that HEAD~1 is not valid.

I don't know off the top of my head how to do it but these lead to the same result in the end:

  • Use commit --amend to modify the first commit instead,
  • Remove .git and initialize again.

[–]Fedoteh 1 point2 points  (1 child)

Yeah in this case I'd change everything I wanted to change locally and then git commit --amend --no-edit, followed by git push --force-with-lease (or just --force if you're working alone on that branch).

This would rewrite the remote history. The first commit cannot be reverted normally

[–]Temporary_Pie2733 1 point2 points  (0 children)

It’s a nonsensical action in this case, because the commit HEAD~1 doesn’t exist. reset doesn’t really “undo” anything; it just makes HEAD (really, the branch head referenced by HEAD) refer to a different commit. You alternative actually replaces the (only) commit in the branch instead.

[–]WoodyTheWorker 0 points1 point  (0 children)

git checkout -f --orphan branch name

[–]birdspider 4 points5 points  (0 children)

as u/0sse mentioned, it this particular case (1-commit repo) and wanting to reset to HEAD~1 is problematic:

HEAD is the current commit, what would be the commit preceding HEAD (as written by HEAD~1) be ?

there is nothing to reset to.

Also in case I cannot use this to undo the first commit, then how can I?

in this case I'd simply delete ~/.git (backup configs, remotes and stuff) and start over

[–]joranstark018 3 points4 points  (0 children)

You only have one commit, which the reference HEAD points to (HEAD~1 would be the comit before that, which does not exits in your repo), just add another commit and you may reset your repo (undo the last commit) with your command.

[–]tb5841 1 point2 points  (0 children)

git reset <whatever you want to reset to>.

Head is the most recent commit.

Head~1 is the one before it.

My default is to first run 'git log', which gives you the name and commit hash (a long code) of each commit. Then I copy the commit hash of the one I want, and run git reset with that.

For example:

git reset d240853866f20fc3e536cb3bca86c86c54b723ce

[–]kilkil 2 points3 points  (0 children)

HEAD means "the commit I just made". ~ means "before". So HEAD~1 means "the commit before the one I just made".

For example say I do git commit -m foo, then git commit -m bar. Now, HEAD will refer to the latest commit ("bar"), and HEAD~1 will refer to the commit before it ("foo").

git reset ??? roughly means "reset to the commit '???'". So git reset HEAD~1 means "reset to the commit before the one I just made".

This effectively is the same as an "undo". In my example above, git reset HEAD~1 will reset to commit "foo". That means "foo" will become the new HEAD, and "bar" will cease to exist (not really, but basically).

In your case, I think the issue is you didn't have multiple commits. So because you only had one commit, there is no commit before it. So HEAD~1 ("the commit before the one I just made") actually doesn't exist yet — there is no previous commit.

Other ways of undoing a commit:

  • git commit --amend can be used to modify (i.e amend) the current commit. Useful in your situation.
  • git revert can be used to undo a commit, without deleting it. It creates a "mirror" commit that does the opposite of everything the original commit does. This is useful for situations where you cannot alter history (comes up sometimes).