2011-02-24 43 views
3

我在分支F​​OO,與父母答:如何根據相同的父項將git提交分成兩部分?

---A---foo 

我想分區從A更改爲foo爲兩個分支,這兩個 基於答:我相信我可以這樣做了一半工作:

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

這給了我:

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

我知道如何得到它的這個(GIT結賬-b otherHalfFoo; git的承諾-a):

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

但我想是讓這樣的:

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

我怎麼能做到這一點,而不必去通過「混帳添加-i」(和 選擇的負什麼被選爲halfFoo)?

+0

沒有'git co'命令。我假設它是基於名稱和用法的'checkout'的別名,但將來請使用完整的命令,而不是您自己的自定義別名。 – 2013-07-14 19:24:14

+0

@Cupcake:謝謝你指出我的錯誤,現在修復了。 – 2015-07-23 14:43:00

回答

3

首先,倒回到先前的提交中,並在此後進行更改。然後分支並應用一些更改。再次從原始分支分支,並應用其餘的更改。

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' 
+0

正是我需要的。謝謝。 – 2011-02-24 02:45:27

0

從你的圖表,從倒數第二圖形到達最後一個圖像,再結賬,創建一個新的分支,摘櫻桃otherHalfFoo。然後重新設置 - 嚴格分支woth halfFoo到正確的提交(HEAD〜1)。

1

最簡單的方法是這樣的:

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

出現提示時,刪除你不想包含提交的線條和標記,你需要用「e」的編輯,而不是「挑那些」。

+0

除非我誤解了某些東西,否則似乎這種技術會要求我經歷兩次所有更改,刪除並標記第一個分支,然後刪除並標記第二個分支的相反部分。 @ wilhelmtell的解決方案似乎更適合我所要做的事情。 – 2011-02-24 12:59:42

+0

他的解決方案假定您想要在補丁中將所有內容添加爲一個提交。我的方法將允許您保留提交,以使2個分支完全按照您的需要顯示,包括歷史記錄。你的解決方案可能沒問題,但對於那些想要分裂成兩個分支的其他人來說,這不起作用。如果你第二次運行git rebase -i A,這個解決方案也適用於你的最終結果。 – 2011-02-24 18:11:07

+0

加上你可以削減你自己,如果你不知道它是如何行事的時候索引,追蹤,但沒有索引和跟蹤。 – 2011-02-24 18:16:23

0

使用checkout -i來交互式地選擇你想要的位在一半,並提交。然後在分支上使用git revert,刪除所有內容。

從分支FOO你的榜樣,與父母答:

---A---foo 

創建一個新的分支和交互方式添加你想要有變化:

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

這給:

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

然後,使用輕鬆創建一個沒有爲halfFoo選擇的項目的foo分支:

git co -b otherHalfFooWithHistory foo 
git revert halfFoo 

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

在概念上是你想要的otherHalfFoo,但確實有所有FOO的存在,然後一些它去掉了歷史。您可以選擇使用merge --squash進行清理:

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

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