¿Hay alguna manera de aplastar un número de confirmaciones de forma no interactiva?


Estoy tratando de aplastar un rango de commits - HEAD to HEAD~3. ¿Hay una forma rápida de hacer esto, o necesito usar rebase interactive interactive?

Author: Karl Bielefeldt, 2011-09-01

8 answers

Asegúrese de que su árbol de trabajo está limpio, entonces

git reset --soft HEAD~3
git commit -m'new commit message'
 110
Author: wilhelmtell,
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
2011-09-01 19:46:25

Personalmente me gusta la solución de wilhelmtell:

git reset --soft HEAD~3
git commit -m 'new commit message'

Sin embargo, hice un alias con algunas comprobaciones de errores para que pueda hacer esto:

git squash 3 'my commit message'

Recomiendo configurar alias que realmente ejecuten scripts para que sea más fácil (a) codificar sus scripts y (b) hacer un trabajo más complejo con la comprobación de errores. A continuación hay un script que hace el trabajo de squash y a continuación hay un script para configurar tus alias de git.

Guión para aplastar (squash.sh)

#!/bin/bash
#

#get number of commits to squash
squashCount=$1

#get the commit message
shift
commitMsg=$@

#regular expression to verify that squash number is an integer
regex='^[0-9]+$'

echo "---------------------------------"
echo "Will squash $squashCount commits"
echo "Commit message will be '$commitMsg'"

echo "...validating input"
if ! [[ $squashCount =~ $regex ]]
then
    echo "Squash count must be an integer."
elif [ -z "$commitMsg" ]
then
    echo "Invalid commit message.  Make sure string is not empty"
else
    echo "...input looks good"
    echo "...proceeding to squash"
    git reset --soft HEAD~$squashCount
    git commit -m "$commitMsg"
    echo "...done"
fi

echo
exit 0

Luego conectar eso squash.sh script para un alias de git, haga otro script para configurar sus alias de git de la siguiente manera ( create_aliases.comando o create_aliases.sh):

#!/bin/sh
echo '-----------------------'
echo 'adding git aliases....'
echo '-----------------------'
echo
git config --global alias.squash "!bash -c 'bash <path to scripts directory>/squash.sh \$1 \$2' -"
#add your other git aliases setup here
#and here
#etc.
echo '------------------------------------'
echo 'here is your global gitconfig file:'
echo '------------------------------------'
more ~/.gitconfig
echo 
echo
echo '----------------'
echo 'end of script...'
echo '----------------'
 27
Author: n8tr,
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-12-08 05:41:36

He utilizado:

EDITOR="sed -i '2,/^$/s/^pick\b/s/'" git rebase -i <ref>

Funcionó bastante bien. Simplemente no intentes tener un registro de confirmación con una línea que comience con "pick":)

 6
Author: Julien Wajsberg,
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-09-20 10:05:37

Use el siguiente comando para aplastar las últimas 4 confirmaciones dentro de la última confirmación:

git squash 4

Con el alias:

squash = !"f() { NL=$1; GIT_EDITOR=\"sed -i '2,$NL s/pick/squash/;/# This is the 2nd commit message:/,$ {d}'\"; git rebase -i HEAD~$NL; }; f"
sq = !git squash $1
sqpsf = !git squash $1 && git psf 

De https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig

 2
Author: brauliobo,
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
2015-03-01 02:13:09

Para agregar a la respuesta de wilhelmtell Me parece conveniente restablecer suavemente a HEAD~2 y luego modificar la confirmación de HEAD~3:

git reset --soft HEAD~2
git commit --all --amend --no-edit    

Esto fusionará todas las confirmaciones con la confirmación de HEAD~3 y usará su mensaje de confirmación. Asegúrese de comenzar desde un árbol de trabajo limpio.

 2
Author: harmonious,
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-09-29 20:09:40

Aquí hay una línea para aplastar las últimas 2 confirmaciones. En este ejemplo, se conservará el mensaje de la penúltima confirmación. Puede cambiar el mensaje como desee.

git commit -am "$(git log -1 --skip=1 --pretty=%B | xargs && git reset --soft HEAD~2)"

Este comando será muy útil si crea un alias para este comando y utilizar el alias en lugar.

 1
Author: Jasir,
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-06-28 12:26:08

Puedes acercarte bastante con

git rebase --onto HEAD~4 HEAD~ master

Esto supone que estás en master con una historia lineal. No es un squash porque descarta los commits intermedios. Tendrías que modificar el nuevo HEAD para modificar el mensaje de confirmación.

 0
Author: Greg Bacon,
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
2011-09-01 19:54:47

Para aplastar todo desde que la rama fue bifurcada del maestro:

git reset --soft $(git merge-base --fork-point master) \
  && git commit --verbose --reedit-message=HEAD --reset-author
 0
Author: Andy,
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-02-12 19:56:49