0

I have a stream of commits. I want to go back and split one up into multiple commits

Imagine I have this:

c5 edit D
c4 edit C
c3 edit A B <- want to split
c2 edit B
c1 edit A

and want this:

c6' edit D
c5' edit C <- replay old c4 & c5
c4' edit B <- commit changes to b
c3' edit A <- commit changes to a
c2  edit B
c1  edit A

Basically I want to go to c3, pop the stack, unstage c3 and split it up, then replay the stack on top of the new head.

I know I can git reset HEAD^ to un-commit a changeset, but how can I perform the pop-and-replay?

1 Answers1

0

First, ensure that the commits to be replayed are backed up. If possible, push them to a local/not public mirror.

I will assume that your current branch is called B1.

Then it's pretty straightforward, and there are a few equivalent solutions

git checkout -b B2 c3      # get to the point to be amended
git reset HEAD~            # this will leave the c3 commit changes uncommitted
git add a                  # add changes in file a
git commit                 # this is your c3' commit
git add b                  # add changes in file b
git commit                 # this is your c4' commit
git cherry-pick c4 c5      # pick the other commits - can be done with rebase --onto too
git diff --name-status B1  # ensure things are the same

It is also possible to do it with the rebase --interactive command, like:

git rebase --interactive c2
# here, mark c3 as 'edit'
# then, save
# you will have shell control in c3, then do as in the previous example:
git reset HEAD~
git add a
git commit
git add b
git commit  # this is probably not necessary, haven't tried
git rebase --continue
# again, ensure things are the same, but now we are in B1 so 
# we compare with the previous commit
git diff --name-status c5
carnicer
  • 494
  • 3
  • 9