开发者

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 partit开发者_如何学Pythonion 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?


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'


The simplest way is this:

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

When prompted, delete the lines that contain the commits you don't want and mark the ones you need to edit with an "e" instead of the "pick".


From your graphs, to get to the last image from the penultimate graph, checkout A again, create a new branch and cherry-pick otherHalfFoo. Then reset --hard the branch woth halfFoo to the correct commit (HEAD~1 probably).


Use checkout -i to interactively select bits you want in one half, and commit that. Then use git revert on the branch with everything to remove what you split out.

From your example on branch foo, with parent A:

---A---foo

Create a new branch and interactively add the changes you want there:

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

That gives:

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

Then, easily create a branch that is foo without the items you selected for halfFoo by using git revert:

git co -b otherHalfFooWithHistory foo
git revert halfFoo

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

Which is conceptually what you want for otherHalfFoo, but does have a history where all of foo existed and then some of it was removed. You can optionally clean it out with merge --squash:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜