¿Editar la confirmación de root en Git?


Hay maneras de cambiar el mensaje de confirmaciones posteriores:

git commit --amend                    # for the most recent commit
git rebase --interactive master~2     # but requires *parent*

¿Cómo puedes cambiar el mensaje de confirmación de la primera confirmación (que no tiene padre)?

Author: Martin G, 2010-01-22

5 answers

Asumiendo que tiene un árbol de trabajo limpio, puede hacer lo siguiente.

# checkout the root commit
git checkout <sha1-of-root>

# amend the commit
git commit --amend

# rebase all the other commits in master onto the amended root
git rebase --onto HEAD HEAD master
 240
Author: CB Bailey,
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
2014-12-17 09:46:37

A partir de la versión de Git 1.7.12, ahora puede usar

git rebase -i --root
 467
Author: ecdpalma,
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
2014-05-14 18:02:51

Para ampliar la respuesta de ecdpalma , ahora puede usar la opción --root para decirle a rebase que desea reescribir la confirmación raíz/primera:

git rebase --interactive --root

Luego la confirmación de root aparecerá en la lista de TAREAS de rebase, y puede seleccionar editarla o reformularla:

reword <root commit sha> <original message>
pick <other commit sha> <message>
...

Esta es la explicación de --root de los documentos de Git rebase (énfasis mío):

Rebase todas las confirmaciones accesibles desde <branch>, en lugar de limitarlas con un <upstream>. Esto le permite reajuste de la raíz(s) en una rama.

 55
Author: Community,
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-05-23 12:18:20

Otra forma de evitar este problema si sabes que estarás rebasando la" primera " confirmación en el futuro, es hacer una confirmación vacía al principio:

git commit --allow-empty -m "Initial commit"

Y solo entonces comienza a hacer commits "reales" , entonces puedes fácilmente rebase encima de ese commit de la manera estándar usando sth como git rebase -i HEAD^

 9
Author: jakub.g,
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
2014-03-06 18:37:01

Podrías usar git filter-branch:

cd test
git init

touch initial
git add -A
git commit -m "Initial commit"

touch a
git add -A
git commit -m "a"

touch b
git add -A
git commit -m "b"

git log

-->
8e6b49e... b
945e92a... a
72fc158... Initial commit

git filter-branch --msg-filter \
"sed \"s|^Initial commit|New initial commit|g\"" -- --all

git log
-->
c5988ea... b
e0331fd... a
51995f1... New initial commit
 4
Author: Alexander Groß,
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
2014-05-25 06:06:20