473

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 shows error - fatal: invalid upstream 'HEAD~3'!

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?


ERROR with git rebase -i HEAD~3:

fatal: invalid upstream 'HEAD~3'  
Gangula
  • 5,193
  • 4
  • 30
  • 59
lxs
  • 8,847
  • 4
  • 20
  • 19
  • 8
    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

818

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 that has since been deleted but is now expanded in https://stackoverflow.com/a/68279810/1256452's answer, 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.) For some purposes, such as splitting what had been the initial commit, this initial blank commit is helpful.

Side note from the future (2022): It's often a good idea to make the very first commit contain just a few boilerplate files like a README. The very first commit in any new, empty repository is always a bit special. Note that if you use hosting sites like Bitbucket, GitHub, and GitLab, they will often make such an initial commit for you when you create a repository there, so that you can clone the repository thus created and have a starting point.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    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
  • 3
    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
  • @Jason The message would be something along the lines of “error: cannot 'fixup' without a previous commit You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'. Or you can abort the rebase with 'git rebase --abort'.”. – kleinfreund Aug 31 '22 at 17:50
23

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 rm -r --cached .
git clean -d -f
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
  • 670
  • 4
  • 16
  • tokek's answer worked fine for me for adding a file. You're either wrong or you're incomplete (the "or anything like that" doesn't include adding/removing files) – Mark Jeronimus Jun 05 '22 at 19:48
  • 1
    This worked like a charm. I think you need to un-stage all the files and run `git clean -fd` before the empty commit step to ensure the commit will be empty. – tarekahf Aug 10 '22 at 20:30
  • Nice pick-up @tarekahf. I've updated the commands to include those steps. – sworisbreathing Aug 16 '22 at 07:12
  • 1
    It *is* possible to split up the root commit without making an extra commit, but this method is likely a lot easier and therefore better for most people. :-) (I got brought back here by a recent comment and just noticed this answer; upvoted now) – torek Aug 31 '22 at 22:16
  • 1
    nb: this might fail because of pre-commit hooks – CervEd Oct 19 '22 at 10:35