How to remove the first commit in git?

I am curious about how to remove the first commit in git.

What is the revision before committing any thing? Does this revision have a name or tag?


For me, the most secure way is to use the update-ref command:

git update-ref -d HEAD

It will delete the named reference HEAD, so it will reset (softly, you will not lose your work) ALL your commits of your current branch.

If what you want is to merge the first commit with the second one, you can use the rebase command:

git rebase -i --root

A last way could be to create an orphan branch, a branch with the same content but without any commit history, and commit your new content on it:

git checkout --orphan <new-branch-name>

There is nothing before the first commit, as every commit is referring a parent commit. This makes the first commit special (an orphan commit), so there is no way to refer to a previous "state".

So if you want to fix the commit, you can simply git commit --amend: this will modify the commit without creating another one.

If you just want to start all over, delete the .git repository, and make another one with git init


# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "Initial commit"

# Delete the old branch:
git branch -D master

# Rename the temporary branch to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

I was looking for a way to undo all git commits from a repo, like they never happened.

Rebasing will work up to a point. However, the very first (chronologically the oldest git commit) is always going to be problematic, since it does not have a parent, so that will error out.

None of these answers quite solved it for me. But after lots of searching and trial-and-error, I found this to work!

git update-ref -d HEAD
git push origin master -f

Hope this helps you. Have a great day.


You might just want to edit your first commit (as there is always a first commit in a git repo). Consider using git commit --amend --reset-author instead of the usual git commit --amend.