299

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?

6

11 Answers 11

558

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>
10
  • 15
    It's too bad that this isn't the accepted answer. Its also too bad that most hits on google more or less say "you can't undo the first commit". This did the trick for me. Thanks!
    – danielpops
    Apr 27, 2016 at 20:25
  • 1
    Thanks @danielpops for your support! It's good to notice that I answered to this question 3 years later. Git has evoled in the meantime.
    – tzi
    Apr 30, 2016 at 12:51
  • I used rebase, marked the second commit as "squash", and then did a --force push to origin. Thanks!
    – Philip Atz
    Jul 25, 2018 at 9:07
  • 2
    git update-ref -d HEAD is the most secure way to remove all history, not just the initial commit. Jan 27, 2019 at 22:37
  • 1
    I did it and it deleted all my files that I added to the first commit. Sad. At least it wasn't much Jul 12, 2020 at 20:43
47

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

3
  • 9
    This answer is incorrect: there IS a way to revert to the state before the first commit. See tzi's answer below. Mar 22, 2016 at 16:25
  • 3
    The --amend also won't update the author properly if it was initially misconfigured. That's what I needed so i used the accepted answer and then just did a new commit instead. Feb 25, 2019 at 0:14
  • I really don't understand on what basis someone concludes there ISN'T an answer, even if there apparently isn't (I'm putting this phrase here because the answer is very old).
    – nomad
    Jan 24, 2021 at 14:56
17
# 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
4
  • 8
    Not an answer: The question was, "how to remove the first commit", and not "how to delete everything except the last commit".
    – peterh
    Oct 8, 2018 at 12:50
  • 2
    I was looking exactly for this. Thanks! Nov 20, 2019 at 11:48
  • 4
    This answer is copied from here.
    – Jason Law
    Dec 14, 2020 at 6:27
  • for me, i wanted to get rid of the .gitignore file in bitbucket, because it was preventing me from doing my first push from my local repo to bitbucket. i solved it by using git push -f origin master to force it. looking back on it now i think i could have solved it by adding a .gitignore to my local repo too
    – luckyguy73
    May 10, 2021 at 0:01
16

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.

3
  • 5
    To be clear for anyone reading this answer: this deletes every commit, but OP only wants to delete the first commit made to a repo; don't run this to delete the first commit because it will delete ALL commits. Jul 11, 2020 at 19:21
  • I am also getting an error while running the commands above "error: src refspec Master does not match any error: failed to push some refs to <my repo>"
    – Jeanne vie
    Nov 15, 2021 at 12:00
  • @JodyBruchon I don’t see how it will delete any commits on the local repo (where it is run). Maybe on the remote which is pushed to. Apr 20 at 21:37
6

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.

2
  • 1
    Not an answer: the question wanted to delete the first commit, and not to modify the properties of the last one.
    – peterh
    Oct 8, 2018 at 12:51
  • @peterh-ReinstateMonica For clarification: Commits can be amended during interactive rebase to change their content completely, but --reset-author needs to be provided to change the author, too. With that, you can completely replace the first commit to your liking. See also the answer of CharlesB. May 2, 2020 at 16:38
3

Another way you can do is:

  1. Checkout to a branch you want to keep (say dev) git checkout dev
  2. Now, delete the branch you want to reset git branch -D master
  3. Now, create an empty branch with the same name git checkout --orphan master

Ofcourse, all of this would depend on your usecase, but if you have more than one branch, deleting the .git directory does not make sense.

2
  • 3
    After doing this, you have to delete the branch in remote before pushing. Otherwise, when trying to push you'll have this error: ! [rejected] master -> master (non-fast-forward) - error: failed to push some refs to '[email protected]:r1/r2.git' - hint: Updates were rejected because the tip of your current branch is behind - hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again... - If you do a git pull all commits come back.
    – dxvargas
    Dec 29, 2016 at 2:24
  • @dxvargas yust do git push -f to FORCE updating the remote branch.
    – Radon8472
    Aug 17, 2021 at 14:03
2

If you want to keep other branches, but for example make the master branch start anew without common history to other branches, one safe way to achieve this is to create a new repository, and push contents of that in your old one:

cd ..
git init newrepo
cd newrepo
# make some initial commits
git push ../oldrepo master:newmaster

This creates newmaster branch in the old repository, with history that is not common with any of the other branches. Of course, you can just overwrite the master as well, with git push -f.

If you want to destroy all branches and all existing content, then just run

rm -rf .git/
3
  • 2
    Not an answer: The OP wanted to remove the first commit, and not to start a new git repo with the current working tree as initial commit.
    – peterh
    Oct 8, 2018 at 12:53
  • @peterh I agree git rebase --root is probably what OP wanted, but considering this answer has two upvotes, I'll refrain from deleting it in case someone still finds it relevant. Oct 9, 2018 at 6:35
  • I think this answer is exactly relevant - if you want to delete the first commit in some branch, and there is only one commit! (But you also want to keep the other commits in other branches; and you also want to do this so that the changes end up in either Bitbucket or Github, not just locally.) I believe this answer is one way to do this, and I think it may be the only way here to do this. (That's because - also as far as I can see - there is no way to force push a local branch with no commits over a remote branch with some commits.)
    – MikeBeaton
    Feb 26, 2019 at 15:45
1

If you have just committed it but not pushed it then just remove .git directory and git init again...

3
  • @peterh Yes it does. You're assuming (I think) that the OP's question is about wanting to delete the first commit but keep subsequent commits. Another possibility (which I think matches the OP's question perfectly well - and which this answer is an answer to) is wanting to delete the first and only commit from a branch, reverting it back to completely empty again.
    – MikeBeaton
    Feb 26, 2019 at 15:52
  • @MikeBeaton You are right. I remove my comment, but then I need to vote to close the question as unclear. Note, although only I was made this comment, but behind me there are 40000 visitors, and a significant part of them found this question with google - and they didn't get what they wanted.
    – peterh
    Feb 26, 2019 at 16:21
  • That seems extreme. Delete the question? Could it not (perfectly well) be argued that a full answer to what is a perfectly reasonable question is that there are two different approaches that need to be taken, depending on whether... etc.? (I mean, I, for one, would like to know how to remove the first commit when it's not the only commit, and also how to remove the first commit when it is the only commit. It's certainly not obvious, and certainly does depend on implementation specific details, that it in fact turns out that really rather different approaches are needed in each case.)
    – MikeBeaton
    Feb 27, 2019 at 17:29
1

None of the answers here do the thing I implicitly understood from the question (And personally needed to do) which is, How to delete the first commit but maintain the rest of the history.

Here's how I did it (inspired by the answer from @Mega Sahu):

  1. Create a backup of your entire repo.

  2. Check out to a temporary branch using switch so that it's completely empty:

    git switch --orphan TMP_BRANCH
  1. Add a temporary file for an initial commit:
    touch tmp
    git add tmp
    git commit -m "initial "
  1. Most Important step: Cherry pick all commits you want except those you don't want (The initial commit and anything else). A GUI is best for this
    gitk
  1. To be safe, compare the repo after cherry picking with the backup you created to make sure everything is in place.

  2. Delete the old branch:

    git branch -D master
  1. Rename the temporary branch to master:
    git branch -m master
0

The answer to the question depends on whether:

  • You want to remove the first AND ONLY commit on a branch (whilst leaving other branches as they were), or whether

  • You want to remove the first commit on some branch, whilst 'leaving in place' the subsequent commits (and the other branches).

The second of these is relatively simpler. You essentially have to rebase on to root - most of the answers here are about ways to do this.

To do the second (removing the first and only commit from a branch whilst leaving other branches alone) is harder. Or, at least, especially harder if you want it to happen and for the change to be reflected back in GitHub or Bitbucket. There is (as far as I can tell) no way at all in Git to push or force push a branch with no commits. And there is also (again, as far as I can see) no way at all to create a new, empty branch with no commits on it in either GitHub or Bitbucket. So you essentially have to create a new repository in order to create a completely empty branch, and then add back the branches which you want (including the commits which you want) - as per @user1338062's answer.

So I hope this answer clarifies what might not have been obvious - that there are two different approaches that need to be taken, for two different (more or less reasonable) scenarios which are both things you might want to be able to do, in order to fully master doing what the OP asks.

0

If it is the first and only commit, git commit --amend can be used to combine the staged changes and modify the most recent, i.e. here the first, commit. The modified commit will be a new one with its own ref.

# stage the new changes which will replace previous commit
git add my_file.py
# commit and change message
git commit --amend -m "Updating first and only commit"
# or commit without changing the message
git commit --amend --no-edit

If nothing new is staged, it just edits previous commit's message.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.