tags:

views:

61

answers:

1

I want to split the first commit in my git repository, but I cannot use rebase to do this because a parent node is required. I found http://stackoverflow.com/questions/2119480/changing-the-message-of-the-first-commit-git useful for modifying the first commit, but not splitting it. How can I split it?

+5  A: 

You can just follow exactly the same process in the question you've linked to, but after checking out the root commit you can use git commit --amend to modify the original commit and then git commit to make an additional commit before continuing the with the rebase command.

Depending on how you want to split the commit you can use git rm --cached to remove files that you want to add at the second commit before the initial git commit --amend and edit any files that you want to look different before calling git add on those files, again before you call git commit --amend.

After calling git commit --amend, to make sure that you commit exactly the state of the original root commit you can call:

git checkout <sha1-of-original-root> -- .

before calling git commit to make the second commit of the split root commit.

Charles Bailey
(rewritten) You do not want `git reset --hard` here. You might use `git reset <sha1-of-original-root> -- .`, but that will not “restore” the working tree, only the index. Probably the best idea is `git checkout <sha1-of-original-root> -- .` which will update the index and working tree but not HEAD (leaving you ready to commit the second part of the split (original root) commit). The main idea here is that both *reset* and *checkout* will change HEAD except when you give them a path argument.
Chris Johnsen
@Chris Johnsen: You're absolutely correct. I was thinking of a two stage reset, path then `--hard` but then didn't actually write that. The checkout solution is better.
Charles Bailey