How to split git commit into two parts based on the same parent?

I'm on foo branch, with parent A:

---A---foo

I want to split the changes from A to foo into two branches, as based on A. I believe that I can do half the work as follows:

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

This gives me:

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

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

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

But I would like to get this:

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

How can I do this without having to go through git add -i '(and selecting a negative result for what was chosen for HalfFoo)?

+3
source share
4 answers

First, rewind the previous commit, leaving after that change. Then open and apply some changes. Open the source 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'
+3

:

git checkout -b branch1 foo
git rebase -i A
git checkout -b branch2 foo
git rebase -i A

, , , , , "e" "pick".

+1

, , A, otherHalfFoo. reset - Foo (, HEAD ~ 1).

0

checkout -i , , . git revert , , .

foo A:

---A---foo

:

git co -b halfFoo foo~
git checkout -i foo
git commit

:

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

, foo , halfFoo, git revert:

git co -b otherHalfFooWithHistory foo
git revert halfFoo

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

Which is conceptually what you want for otherHalfFoo, but has a story in which all foo exists, and then some of them were deleted. You can optionally clean it with merge --squash:

git co -b otherHalfFoo foo~
git merge --squash otherHalfFooWithHistory
git branch -D otherHalfFooWithHistory

---A---foo
   |\
   | \-halfFoo
    \
     \-otherHalfFoo
0
source

Source: https://habr.com/ru/post/1794300/


All Articles