2013-02-04 19 views
17

Un repository con due rami.Git: sposta il commit specifico su un altro ramo

ramo principale si impegna:

c1, c2, c3, c4, c5, c6, c7, ..., C15, ...

Staging Branch si impegna:

c1, c2 , C3, C4, C5, C6, C7

voglio spostare tutti impegna dal ramo principale dopo c7 alla messa in scena ramo

e poi tornare Mas ramo ter

con

git reset --hard c7-hash 

Come spostare/copiare impegna specifici da un ramo all'altro?

+3

In questo caso non è necessario spostare i commit perché il ramo di staging può essere inoltrato rapidamente padroneggiare. 'git checkout staging', ' git merge master', 'git checkout master', ' git ripristinare --hard C7-hash' – tewe

+0

possibile duplicato di [Come spostare alcuni si impegna ad un altro ramo in git?] (http://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-another-branch-in-git) – Kevin

+0

possibile duplicato di [Come posso spostare i commit recenti su una nuova diramazione con git?] (http://stackoverflow.com/questions/1628563/how-can-i-move-recent-commits-to-a-new-branch-with-git) –

risposta

35

Nel caso che hai descritto, in cui tutti i commit sul ramo messa in scena sono anche sul ramo maestro, è molto facile:

git checkout staging 
git merge master 
git checkout master 
git reset --hard c7-hash 

La fusione sarà un fast-forward.

Nel caso generale, è possibile utilizzare git cherry-pick c8 c9 c10 c11 c12 c13 c14 c15 per selezionare i singoli commit nel ramo corrente. Un modo più breve per selezionare tutti i commit che sono sul master ma non il ramo corrente è git cherry-pick ..master, e ci sono altri esempi mostrati da git help cherry-pick

+0

Dopo aver fatto ciò, diciamo che ti impegni " x' al ramo principale. E poi unisci il master alla messa in scena. Sarebbe unire in staging in modo corretto unire 'x' in staging mentre _not_ ripristina c8 ... c15 in staging a causa del fatto che quei commit erano già resettati in master? Vuoi garantire che c8 ... c15 rimanga in scena dopo la fusione. –

+0

@MarcusLeon, la fusione non ripristina tali commit. Questi commit non sono stati ripristinati in master, sono stati rimossi completamente. Un reset non è un ripristino. È facile creare test di commit e testare scenari del genere, provalo. –