Introduccion
Mantener sincronizados varios repositorios Git suele ser una tarea manual y propensa a errores. Este enfoque usa hooks para automatizar la sincronizacion entre dos repositorios identicos.
Casos de uso
- Mantener un repositorio de respaldo sincronizado con la cuenta principal.
- Mantener forks o mirrors consistentes en otra cuenta.
- Actualizar automaticamente repositorios de demo o testing.
Prerrequisitos
Los dos repositorios deben tener la misma estructura. Las ramas y cambios del principal deben reflejarse en el secundario.
Paso a paso
Hook pre-commit
Prepara la sincronizacion copiando archivos staged del repo principal al secundario.
#!/bin/bash
# Path to the secondary (backup) repository
BACKUP_REPO_DIR="/path/to/backup/repo"
# Copy staged files to the secondary repository
git diff --cached --name-only --diff-filter=ACM | xargs -I {} cp {} $BACKUP_REPO_DIR
Hook post-commit
Captura mensaje y autor del commit del repo principal y lo replica en el secundario.
#!/bin/bash
# Author for the commits in the secondary repository
AUTHOR="[email protected]"
# Path to the secondary repository
BACKUP_REPO_DIR="/path/to/backup/repo"
# Capture the message of the last commit
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
# Go to the secondary repository and commit with the captured message
cd $BACKUP_REPO_DIR
git config user.email "$AUTHOR"
git add .
git commit -m "$COMMIT_MESSAGE"
# Amend the last commit to change the author
git commit --amend --author="$AUTHOR" --no-edit
Hook pre-push
Finaliza la sincronizacion enviando los cambios del repo secundario a su remoto.
#!/bin/bash
# Path to the secondary repository
BACKUP_REPO_DIR="/path/to/backup/repo"
# Go to the secondary repository and push the changes
cd $BACKUP_REPO_DIR
git push
Conclusion
Automatizar sincronizacion con hooks reduce trabajo manual, evita errores y mantiene consistencia entre repositorios paralelos.
Happy reading! ☕
Comments