Cómo deshacer "git commit --amend" hecho en lugar de "git commit"

Resuelto Jesper Rønn-Jensen asked hace 14 años • 14 respuestas

Accidentalmente modifiqué mi confirmación anterior. La confirmación debería haber sido separada para mantener el historial de los cambios que hice en un archivo en particular.

¿Hay alguna manera de deshacer ese último compromiso? Si hago algo como git reset --hard HEAD^, la primera confirmación también se deshace.

(Aún no he enviado a ningún directorio remoto)

Jesper Rønn-Jensen avatar Sep 22 '09 16:09 Jesper Rønn-Jensen
Aceptado

Lo que debe hacer es crear una nueva confirmación con los mismos detalles que la HEADconfirmación actual, pero con el padre como la versión anterior de HEAD. git reset --softmoverá el puntero de la rama para que la siguiente confirmación ocurra encima de una confirmación diferente de donde se encuentra ahora el encabezado de la rama actual.

# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before 
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}

# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
CB Bailey avatar Sep 22 '2009 10:09 CB Bailey

Utilice el registro de referencia :

git branch fixing-things HEAD@{1}
git reset --soft fixing-things

Luego debería tener todos los cambios modificados previamente en su copia de trabajo y poder enviarlos a una nueva confirmación.

Para ver una lista completa de confirmaciones principales anteriores, escriba git reflog.

Es posible que mi otra respuesta le resulte útil si está buscando un comando único para lograr esto y no le temen a los comandos de plomería de bajo nivel.

knittl avatar Sep 22 '2009 10:09 knittl

Ninguna de estas respuestas con el uso de HEAD@{1}funcionó para mí, así que aquí está mi solución:

git reflog

d0c9f22 HEAD@{0}: commit (amend): [Feature] - ABC Commit Description 
c296452 HEAD@{1}: commit: [Feature] - ABC Commit Description 

git reset --soft c296452

Su entorno de prueba ahora contendrá todos los cambios que fusionó accidentalmente con la confirmación c296452.

Oisín Foley avatar Jan 30 '2021 19:01 Oisín Foley

Encuentre sus confirmaciones modificadas por:

git log --reflog

Nota: Puede agregar --patchpara ver el cuerpo de las confirmaciones para mayor claridad. Igual que git reflog.

luego restablezca su HEAD a cualquier confirmación anterior en el punto en que estaba bien:

git reset SHA1 --hard

Nota: Reemplace SHA1 con su hash de confirmación real. También tenga en cuenta que este comando perderá todos los cambios no confirmados, por lo que puede guardarlos antes. Como alternativa, utilícelo --soften su lugar para conservar los últimos cambios y luego confirmarlos.

Luego, seleccione el otro compromiso que necesita encima:

git cherry-pick SHA1
kenorb avatar Mar 27 '2016 01:03 kenorb

Si envió la confirmación a control remoto y luego modificó erróneamente los cambios en esa confirmación, esto solucionará su problema. Emita a git logpara encontrar el SHA antes de la confirmación. (esto supone que el control remoto se llama origen). Ahora emita estos comandos usando ese SHA.

git reset --soft <SHA BEFORE THE AMMEND>
#you now see all the changes in the commit and the amend undone

#save ALL the changes to the stash
git stash

git pull origin <your-branch> --ff-only
#if you issue git log you can see that you have the commit you didn't want to amend

git stash pop
#git status reveals only the changes you incorrectly amended

#now you can create your new unamended commit
David Sopko avatar Apr 24 '2019 18:04 David Sopko