Reputation: 1036
I'm rebasing, and I have two commits A and B that I'd like to apply in the opposite order, B then A, but they conflict. Is there a way to split the second commit B into two commits: B1 containing only hunks that do not conflict with A, and B2 containing only the conflicting hunks?
I used to have my own patch management tool that had a sift
command that did this for me. Git is better than my tool in every way, of course, but I miss this functionality.
Upvotes: 0
Views: 102
Reputation: 4633
Interactive rebase itself (as I assume you are already using) will help you solve this smoothly. For the completeness of this answer I'll note that more info can be found by looking for the --interactive
(or -i
) option at man git-rebase.
A rough example:
git rebase -i HEAD~5 # rebase starting on some earlier commit
This brings up your editor, with each commit marked pick
. Change the one you wish to split to instead say edit
, then save and close the file.
The rebase will stop at right after this commit, allowing you to amend or otherwise alter the history. In your case, you will want to reset to the commit before it, and then make new commits in its place, then continue the rebase procedure. You can use the --patch
or -p
option to git add
to add a file partially.
git reset HEAD^
git add -p <file> # -p is optional, use if needed
git commit -m 'First part'
git add -p <file> # -p is optional, use if needed
git commit -m 'Second part'
It is a good idea to double-check that the end result matches what you had before. Git will set ORIG_HEAD
to point to the commit you had before you started the rebase, so you can simply diff against this to see that there is no change (or that the change corresponds to anything you have purposely done in the rebase).
git diff ORIG_HEAD
I hope this helps.
Upvotes: 1