Students in my Git Course raised an interesting and tricky challenge yesterday when we were working through the rebasing stuff:

“We accidently added a big file to our first (root) commit? And we’ve laid down a bunch more commits since. How do you rebase your root commit?”

What a great question! Normally you rebase the children of a particular commit. But what do you do when you want to rebase the very first commit? Messy.


flickr photo shared by derekbruff under a Creative Commons ( BY-NC ) license

Enter the –root

As covered in this Stack Overflow, all recent Git versions have a new --root switch to rebase for just such a scenario.

Aside from the magic --root switch, it’s a standard rebase operation:

  • Change to master and git rebase -i --root to start an interactive rebase of root

  • When prompted for the rebase order, set the first commit to edit as shown below

    edit 064a191 First commit with dodgy file
    pick 8a673aa Second commit with normal stuff

  • The rebase will stop on the first commit which we’ve marked for edit, letting you git rm dodgy.mp4 to get rid of your file

  • Once that’s done, run a git commmit --amend to update your message

  • And then run a git rebase --continue to let the rebase complete normally

And you have yourself a nice clean root commit free from large mp4s!

Just writing it down so I can remind myself later!