2010-07-02 87 views
41

我在直線一直在努力:混帳:如何某些提交移動到新的分支

A---B---C---D---E---F (master:HEAD) 

現在我想向後移動:

git checkout C 

和移動一些最後提交到一個新的分支:

選項1:

  D---E---F (new:HEAD) 
     /
A---B---C (master) 

選項2:

  F (new:HEAD) 
     /
A---B---C (master) 

如何重定向到選項1以及如何選項2?

回答

70

要從第一圖(主= HEAD = F)到達選項1:

git branch new  # Make a 'new' branch pointing at HEAD, which is F 
git reset --hard C # Move master back to point at C 
git checkout new  # Make HEAD follow new, and get F in the working tree 

而從選項1至選項2(拿起其中上述左關閉),

git rebase -i master # Start the process of re-jiggering this branch 
# edit the commit list that opens up to only include F 
# save and exit 
# resolve potential conflicts from missing changes in D and E 

要直接從您的出發點去選擇2:

git checkout -b new C # Start the 'new' branch at C 
git cherry-pick F  # Include F on it 
git checkout master # Switch back to master 
git reset --hard C  # Rewind master to the earlier commit 
+2

感謝這個詳細的解答。 – takeshin 2010-07-02 19:19:32

+1

謝謝:)另外,爲「重新jiggering」+1! – dokkaebi 2012-12-21 22:54:58

+3

如果在嘗試推送主服務器時出現錯誤「更新被拒絕,因爲當前分支的提示位於其遠程對象的後面」,則需要使用--force選項:'git push --force origin master' – Tamlyn 2013-02-27 11:18:17