¿Cómo mover ciertas confirmaciones a otra rama en git?


La situación:

  • el maestro está en X
  • quickfix1 está en X + 2 commits

Tal que:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

Entonces empecé a trabajar en quickfix2, pero por accidente tomé quickfix1 como la rama fuente para copiar, no el maestro. Ahora quickfix2 está en X + 2 commits + 2 commits relevantes.

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

Ahora quiero tener una rama con quickfix2, pero sin las 2 confirmaciones que pertenecen a quickfix1.

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

Traté de crear un parche a partir de una cierta revisión en quickfix2, pero el parche no conserva el historial de confirmaciones. ¿Hay alguna forma de guardar mi historial de confirmaciones, pero tener una rama sin cambios en quickfix1?

Author: Steve Eynon, 2010-03-03

5 answers

Este es un caso clásico de rebase --onto:

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

Así que deberías ir desde

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

A:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

Esto se hace mejor en un árbol de trabajo limpio.
Véase git config --global rebase.autostash true, especialmente después de Git 2.10.

 309
Author: VonC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-11 16:44:03

Puedes usar git cherry-pick para elegir el commit que quieres copiar.

Probablemente la mejor manera es crear la rama desde master, entonces en esa rama usa git cherry-pick en las 2 confirmaciones de quickfix2 que quieras.

 127
Author: DJ.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-03 07:37:34

Lo más simple que puedes hacer es elegir un rango. Hace lo mismo que el rebase --onto pero es más fácil para los ojos:)

git cherry-pick quickfix1..quickfix2
 110
Author: Christoph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-15 06:38:44

Creo que es:

git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
 22
Author: Matthew Flaschen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-15 05:14:13

La única manera de trabajar que encontré:

git show>~/some_file.txt
git checkout master
git checkout -b new_branch
git apply -stat ~/some_file.txt
git apply -check ~/some_file.txt
git apply ~/some_file.txt

Entonces necesitas confirmar los cambios de nuevo, pero es mucho más fácil hacerlo manualmente...

 -3
Author: Ziv Barber,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-31 13:18:13