Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I git rebase the first commit?

Tags:

git

git-rebase

People also ask

Can you squash the first commit?

Using the --root option with the rebase command, we were able to squash the first two commits into a single one.

Can you rebase a single commit?

Rebasing can be helpful to squash the commits into one commit and can also be used to split into multiple commits if you want the clean history of each fix that you want to track.


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.)


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.