Rebase una sola confirmación de Git


¿Hay alguna manera de rebase un solo commit de una rama a otra rama?

Tengo esta estructura de ramas:

-- -- -- -- -- (Master)
            \
              -- -- -- -- -- XX (Feature-branch)

Todo lo que quiero hacer es rebase la última confirmación de Feature-branch en master y revertir Feature-branch una confirmación.

-- -- -- -- -- XX (Master)
            \
              -- -- -- -- -- (Feature-branch)

¿Cómo hago eso?

 78
Author: Lii, 2013-02-01

4 answers

Puedes elegir XX para dominar.

git checkout master
git cherry-pick <commit ID of XX>

Y elimina la última confirmación de la rama feature con git reset.

git checkout Feature-branch
git reset --hard HEAD^
 81
Author: tewe,
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
2013-01-31 21:46:50
git rebase --onto master branch~1 branch 

Esto dice "rebase el rango de commits entre last-before-branch y branch (es decir, XX commit) en la punta de la rama master"

Después de esta operación branch la punta se mueve al confirmar XX, por lo que desea volver a configurarla con

git checkout branch
git reset --hard branch@{1}^

Que dice "restablecer la punta de rama a la confirmación antes de su estado anterior"

Así que un cherry pick es una solución más simple...

 64
Author: CharlesB,
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
2013-01-31 22:09:35

Es bastante simple de hacer en realidad. La solución es hacer un rebase interactivo y "soltar" todas las confirmaciones que no desea incluir en el rebase.

git rebase -i <target_branch> donde target_branch es la rama en la que desea rebase

Entonces editará el archivo que se abre y pick las confirmaciones que desea y drop (o d para abreviar) todas las confirmaciones que no desea traer.

 13
Author: hrdwdmrbl,
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-10-26 19:53:15

@Charles la respuesta es correcta. De todos modos terminé usando esto muchas veces, sobre todo para rebase configuración específica en un proyecto

  * a8f9182 (HEAD -> production) production configuration
  | * daa18b7 (pre) preproduction configuration
  |/  
  | * d365f5f (local) local configuration
  |/  
  * 27d2835 (dev) amazing new feature that will save the world
* | 56d2467 (master) boring state of the art for project
|/

Que cree un nuevo comando para él:

$ cat ~/bin/git-rebaseshot 
COMMIT=$1
DEST=${2:-HEAD}
git rebase ${COMMIT}^ ${COMMIT} --onto $DEST

Normalmente desea autocompletar los nombres de rama para ese comando, así que agréguelo sourcing esta función (adding to.bashrc or .perfil):

_git_rebaseshot () 
{ 
    __gitcomp_nl "$(__git_refs)"
}

Git autocomplete lo buscará

Puedes usar este comando así:

# rebase config on prepro on actual HEAD
$ git rebaseshot prepro 
# rebase config on local onto dev
$ git rebaseshot local dev
# rebase production config on master
$ git rebaseshot pro master

Cuando divide las entidades correctamente, las posibilidades son infinitas.

* a8f9182 (HEAD -> postgres) BBDD config
* a8f9182 (local) local config
* a8f9182 (debug) log level config
* a8f9182 (dev) new feature
|

Supongo que esto es lo que quilt a la gente le gusta hacer.

Este comando funcionará de todos modos con cualquier sha / ref que proporcione:

$ git rebaseshot <Feature branch> master
$ git rebaseshot <commit of XX> master
 1
Author: albfan,
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
2016-05-11 09:32:42