226

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?

hippietrail
  • 13,703
  • 15
  • 87
  • 133
Weihang Jian
  • 5,390
  • 4
  • 33
  • 47
  • 2
    For a start with git, you should know that revision is meaningless. You can talk about commits or their relative SHAs. Also, why would you want to do this? The first commit is what everything is built on. You could squash a few commits together, including the first commit, which becomes the new first commit, but what does it mean to even delete the first commit (or delete any but the last commit)? – Shahbaz Jun 06 '12 at 09:22
  • @Shahbaz yeah, thats the best way, look here for example: http://ariejan.net/2011/07/05/git-squash-your-latests-commits-into-one – timaschew Jun 06 '12 at 09:31
  • 7
    You can use `git rebase -i --root`. See the following SO answer for details: http://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git – MKroehnert Jun 03 '13 at 13:21
  • possible duplicate of [Can I remove the initial commit from a Git repo?](http://stackoverflow.com/questions/575694/can-i-remove-the-initial-commit-from-a-git-repo) – Gordon Gustafson Apr 27 '14 at 18:47
  • [This answer](https://stackoverflow.com/a/6149972/1906641) has the correct solution to deleting the root commit of the current branch: `git filter-branch --parent-filter "sed 's/-p //'" HEAD` – Jody Bruchon Jul 11 '20 at 19:28
  • Does this answer your question? [How to revert initial git commit?](https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit) – Nayab Samar Jan 20 '21 at 19:05

9 Answers9

402

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>
tzi
  • 6,629
  • 1
  • 20
  • 40
  • 10
    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 '16 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 '16 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 '18 at 09:07
  • 2
    git update-ref -d HEAD is the most secure way to remove all history, not just the initial commit. – user1767316 Jan 27 '19 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 – Vyacheslav Tsivina Jul 12 '20 at 20:43
  • thanks, removed most of the files from directory :P – sKopheK Jan 19 '21 at 20:18
45

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

CharlesB
  • 75,315
  • 26
  • 174
  • 199
  • 8
    This answer is incorrect: there IS a way to revert to the state before the first commit. See [tzi's answer](http://stackoverflow.com/a/32765827/46783) below. – David Nelson Mar 22 '16 at 16:25
  • 2
    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. – Mark Edington Feb 25 '19 at 00: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 '21 at 14:56
15
# 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
agupta
  • 168
  • 2
  • 12
Megha Sahu
  • 227
  • 2
  • 2
  • 6
    **Not an answer:** The question was, "how to remove the first commit", and not "how to delete everything except the last commit". – peterh Oct 08 '18 at 12:50
  • 1
    I was looking exactly for this. Thanks! – Krzysztof Tomaszewski Nov 20 '19 at 11:48
  • 1
    This answer is copied from [here](https://stackoverflow.com/a/26000395/5232255). – Jason Law Dec 14 '20 at 06: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 '21 at 00:01
7

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.

Max Beikirch
  • 1,789
  • 1
  • 18
  • 33
  • 1
    **Not an answer:** the question wanted to delete the first commit, and not to modify the properties of the last one. – peterh Oct 08 '18 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. – Max Beikirch May 02 '20 at 16:38
5

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.

kp123
  • 574
  • 9
  • 15
  • 3
    **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. – Jody Bruchon Jul 11 '20 at 19:21
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/
user1338062
  • 9,351
  • 3
  • 54
  • 56
  • 1
    **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 08 '18 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. – user1338062 Oct 09 '18 at 06: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 '19 at 15:45
2

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.

kumarharsh
  • 17,121
  • 7
  • 69
  • 93
  • 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 'git@github.com: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 '16 at 02:24
0

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

Ali Sajid
  • 221
  • 1
  • 11
  • @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 '19 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 '19 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 '19 at 17:29
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.

MikeBeaton
  • 2,077
  • 2
  • 26
  • 36