0

I am aware that we can undo like this : How do I undo the most recent local commits in Git?

However if the commit we want to undo is the first one in the project we got this error message:

git add file

git commit -m "first commit"

git reset HEAD~1

fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.

beatrice
  • 3,684
  • 5
  • 22
  • 49
  • Does this answer your question? [How to squash all git commits into one?](https://stackoverflow.com/questions/1657017/how-to-squash-all-git-commits-into-one) – phd Dec 14 '19 at 10:44
  • I try to always make the first commit in a new repository empty for this reason :-) – oyvind Dec 14 '19 at 10:58
  • @oyvind: An initial commit consisting solely of a README and/or LICENSE file is pretty good practice. – torek Dec 14 '19 at 18:27

4 Answers4

1

Reverting back your first commit is just like creating an empty git repo. Delete the .git folder and initiate a new one

AbdulKarim
  • 605
  • 5
  • 18
1

You can remove HEAD and restore your repository to a new state, where you can create a new initial commit:

git update-ref -d HEAD

After creating a new commit, if you have already pushed to the remote server, you will need to force it to the remote in order to overwrite the previous initial commit:

git push --force origin
1
  • Using git update-ref -d will revert the initial commit.
  • Point to be noted here is it keeps all previously committed changes added to the index.
  • In order to remove these changes,run git reset --hard.
Arcot Deepika
  • 445
  • 4
  • 17
1

You could just set the working tree the way you want for the first revisión, add everything, then:

git commit --amend -m "first commit"

And then the old first commit is gone.

eftshift0
  • 26,375
  • 3
  • 36
  • 60