344

I used git init to create a fresh repo, then made three commits. Now I want to rebase to go back and amend my first commit, but if I do git rebase -i HEAD~3 it complains! If I try the same with HEAD~2 then it kinda works but only lets me rearrange the last two commits.

How do I refer to the 'commit before there were any commits' or go back and insert an empty commit?

lxs
  • 7,327
  • 4
  • 19
  • 19
  • 7
    Possible duplicate of [Edit the root commit in Git?](https://stackoverflow.com/questions/2119480/edit-the-root-commit-in-git) – Liam May 01 '19 at 10:16
  • [Checkout the first commit and amend it](https://stackoverflow.com/a/2119656/9157799) – M Imam Pratama May 23 '22 at 06:35

2 Answers2

607

The easy way, with a recent-enough git (this has been out for a long time now so you should have this):

git rebase -i --root

The other easy way, as twalberg noted in a comment, is to use git checkout --orphan to set up to make a new root commit, which you can copy the old commits on top of. (This is what rebase -i --root ends up doing internally anyway.)

Community
  • 1
  • 1
torek
  • 389,216
  • 48
  • 524
  • 664
  • Using `git rebase -i --root` I get the error `error: cannot 'fixup' without a previous commit` when attempting to squash the second commit (I just want the first) – mikemaccana Apr 11 '19 at 21:14
  • 2
    What exactly are you editing into the instruction sheet? You should have a list of commits, with oldest at the top, and command `pick` for each one. Change the *second* `pick` to either `squash` or `fixup`, write out the instruction sheet, exit your editor, and Git should do the job. – torek Apr 11 '19 at 21:46
  • This is blowing up for me too. What version of git does this work on? – Jason Mar 18 '22 at 17:32
  • @Jason: it's been in since 1.6.2 or 1.7.12 (the basic `--root` went in with Git 1.6.2, an improved version went in at 1.7.12). What do you mean by "blowing up"? – torek Mar 19 '22 at 01:13
  • After farting around with different strategies for 20 minutes the error cleared itself. I have unfortunately lost the exact message. Something about todo. – Jason Mar 23 '22 at 18:44
  • Love this command. Never been able to get it to work without modifying the metadata of all subsequent commits. Though I suppose that could be considered a feature. – vhs May 22 '22 at 03:31
14

torek's answer is fine if you want to make changes to files that are already in the commit, edit the author/message, etc. But if you want to split the commit or anything like that, then chances are you're going to run into trouble because you can't do git reset HEAD~ from the initial commit.

To make this possible, you can insert an empty initial commit like so:

git checkout --orphan empty-initial-commit
git commit --allow-empty -m 'Empty initial commit'
git checkout <branch>
git rebase empty-initial-commit
git branch -d empty-initial-commit

then you can do git rebase -i, edit the commit (the first non-empty commit), and do git reset HEAD~ like normal.

sworisbreathing
  • 579
  • 3
  • 16