How to remove the first commit in git?
Asked Answered
C

11

305

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?

Courland answered 6/6, 2012 at 9:14 Comment(6)
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)?Chaste
@Chaste yeah, thats the best way, look here for example: ariejan.net/2011/07/05/git-squash-your-latests-commits-into-oneSelfsown
You can use git rebase -i --root. See the following SO answer for details: stackoverflow.com/questions/2246208/…Lagunas
possible duplicate of Can I remove the initial commit from a Git repo?Grippe
This answer has the correct solution to deleting the root commit of the current branch: git filter-branch --parent-filter "sed 's/-p <the_commit>//'" HEADMalia
Does this answer your question? How to revert initial git commit?Daysidayspring
B
569

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>
Bibliophile answered 24/9, 2015 at 15:55 Comment(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!Oversexed
Thanks @Oversexed for your support! It's good to notice that I answered to this question 3 years later. Git has evoled in the meantime.Bibliophile
I used rebase, marked the second commit as "squash", and then did a --force push to origin. Thanks!Popcorn
git update-ref -d HEAD is the most secure way to remove all history, not just the initial commit.Instable
I did it and it deleted all my files that I added to the first commit. Sad. At least it wasn't muchPutative
thanks, removed most of the files from directory :PGymnasiast
This didn't help me. I've accidentally pushed my first commit to the main branch instead of to a feature branch, and I wanted to get the main branch in its previous state (i.e. no commits). After running git update-ref -d HEAD, I'm no longer able to push anythingMacias
> I did it and it deleted all my files that I added to the first commit. Sad. At least it wasn't much This happened to me too. git reset --hard fixed it though in my casePlumcot
Thanks alot, My favorite to me is the orphan way, very clean!Dallis
Warning: This will remove HEAD in submodules as well. Use git restore path/to/submodule to restore it (you will lose your work if you didn't commit changes to the submodule [learned the hard way])Liturgical
C
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

Calaboose answered 6/6, 2012 at 9:27 Comment(3)
This answer is incorrect: there IS a way to revert to the state before the first commit. See tzi's answer below.Jochebed
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.Glosseme
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).Syzran
D
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
Devolve answered 28/7, 2017 at 10:47 Comment(4)
Not an answer: The question was, "how to remove the first commit", and not "how to delete everything except the last commit".Aberdeen
I was looking exactly for this. Thanks!Philippines
This answer is copied from here.Dragon
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 tooNardone
M
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.

Marx answered 22/10, 2019 at 5:20 Comment(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.Malia
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>"Hasan
@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.Eiser
T
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.

Twice answered 2/5, 2013 at 20:27 Comment(2)
Not an answer: the question wanted to delete the first commit, and not to modify the properties of the last one.Aberdeen
@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.Twice
K
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.

Khorma answered 8/8, 2013 at 16:57 Comment(2)
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.Concessionaire
@Concessionaire yust do git push -f to FORCE updating the remote branch.Trillby
P
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/
Publishing answered 6/6, 2012 at 9:31 Comment(3)
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.Aberdeen
@Aberdeen 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.Publishing
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.)Octavla
E
2

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
Ehtelehud answered 26/5, 2023 at 16:44 Comment(0)
S
1

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

Sailesh answered 27/8, 2018 at 11:19 Comment(3)
@Aberdeen 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.Octavla
@Octavla 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.Aberdeen
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.)Octavla
O
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.

Octavla answered 27/2, 2019 at 17:45 Comment(0)
S
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.

Stepdaughter answered 19/4, 2023 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.