Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partition a git commit in two, based on same parent?

I'm on branch foo, with parent A:

---A---foo

I want to partition the changes from A to foo into two branches, both based on A. I believe I can do half the job like this:

git checkout -b halfFoo
git reset HEAD~
    # Only choose half the stuff.
git add -i
git commit

That gives me:

---A---foo
    \
     \-halfFoo

I know how to get it to this (git checkout -b otherHalfFoo; git commit -a):

---A---foo
    \
     \-halfFoo
      \
       \-otherHalfFoo

But what I'd like is to get this:

---A---foo
   |\
   | \-halfFoo
    \
     \-otherHalfFoo'

How can I do this without having to go through the 'git add -i' (and selecting the negative of what was chosen for halfFoo) again?

like image 631
Luc St-Louis Avatar asked Feb 18 '26 13:02

Luc St-Louis


1 Answers

First, rewind to your previous commit, leaving in the changes since. Then branch off and apply some of the changes. Branch off from the original branch again, and apply the rest of the changes.

git branch original_tip_with_foo  # if you want a reference to the foo commit
git reset HEAD~
git stash
git branch branch_2  # we'll get there in a moment ...
git checkout -b branch_1
git stash pop
git add -p
# ... add your first set of changes, for the first branch ...
git commit -m'first set of changes'
git stash
git checkout branch_2
git stash pop
git add -p
# ... add your second set of changes ...
git commit -m'second set of changes'
like image 183
wilhelmtell Avatar answered Feb 21 '26 15:02

wilhelmtell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!