gitmacosgithub

Git removing the last N commits?


According to this question we should be able to remove the last N commits like this (Example uses 2 commits):

git reset --hard HEAD~2
or
git reset --hard HEAD^^

However when I try this it produces this error (This is a complete rundown):

 % touch f && git add . && git commit -m "M"
[master (root-commit) fc2ff7f] M
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 f
 % touch f2 && git add . && git commit -m "M"
[master 6017bff] M
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 f2
 % git log
commit 6017bff2f76fe15e6e7c99e3d386ef12db4d3ffb (HEAD -> master)
Author: Ole Ersoy <ole.ersoy@gmail.com>
Date: Wed Dec 6 14:17:11 2023 -0500

    M

commit fc2ff7fc92eebb6c7384d9887faf9c608c891ae0
Author: Ole Ersoy <ole.ersoy@gmail.com>
Date: Wed Dec 6 14:17:06 2023 -0500

    M

 % git reset --hard 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>...]'

Thoughts?


Solution

  • You cannot reset to an empty git repository. You must have at least 3 commits in your repository in order to remove two commits.

    Add one more commit and then try either command again.