Día 26 - Agregar un remote adicional en Git y pushear a él
Problema / Desafío
El equipo de DevOps agregó nuevos remotes en el servidor Git. Se necesita actualizar el repositorio /usr/src/kodekloudrepos/cluster con lo siguiente:
- Agregar un nuevo remote
dev_clusterapuntando a/opt/xfusioncorp_cluster.git - Copiar
/tmp/index.htmlal repo, agregar y commitear enmaster - Pushear
masteral nuevo remotedev_cluster
Conceptos clave
git remote — gestión de remotes
Un repositorio Git puede tener múltiples remotes. Cada remote es un alias que apunta a una URL (o ruta local). El nombre origin es solo una convención — no tiene nada de especial frente a cualquier otro nombre:
# Ver remotes actuales
git remote -v
# Agregar un remote nuevo
git remote add <nombre> <url-o-ruta>
# Eliminar un remote
git remote remove <nombre>
# Renombrar un remote
git remote rename <nombre-viejo> <nombre-nuevo>
Múltiples remotes: casos de uso
| Caso | Configuracion tipica |
|---|---|
| Fork workflow | origin = tu fork, upstream = repo original |
| Deploy a multiples entornos | origin = repo principal, staging = servidor staging |
| Mirror/backup | origin = GitHub, backup = servidor interno |
| Este reto | origin = /opt/cluster.git, dev_cluster = /opt/xfusioncorp_cluster.git |
git push <remote> <branch>
git push sin argumentos empuja al upstream del branch actual (generalmente origin). Para especificar a cuál remote pushear:
git push dev_cluster master # empuja master al remote dev_cluster
git push origin master # empuja master al remote origin
Ambos remotes son independientes — un push a dev_cluster no afecta a origin ni viceversa.
git commit -am vs git add + git commit
# -a agrega automaticamente todos los archivos tracked modificados
# -m especifica el mensaje de commit
git commit -am 'mensaje'
# Equivalente explícito
git add archivo.html
git commit -m 'mensaje'
El flag -a solo funciona con archivos que Git ya trackea (aparecen en git status como modified). Para archivos nuevos (untracked) se necesita git add explícito primero — aunque en este caso cp creó un archivo nuevo, Git lo trackeó correctamente porque el commit fue justo despues del cp.
Nota:
git commit -amincluye archivosuntrackedrecien copiados solo si Git los detecta como parte del working tree en ese momento. Para mayor seguridad, usargit add+git commit -m.
Pasos
- Conectarse al Storage Server y elevar privilegios con
sudo su - Navegar al repositorio clonado
- Verificar los remotes existentes
- Agregar el nuevo remote
dev_cluster - Copiar
index.htmly commitear enmaster - Pushear
masteral nuevo remote
Comandos / Código
1. Conectarse y navegar al repo
2. Verificar remotes existentes
3. Agregar el nuevo remote
dev_cluster /opt/xfusioncorp_cluster.git (fetch)
dev_cluster /opt/xfusioncorp_cluster.git (push)
origin /opt/cluster.git (fetch)
origin /opt/cluster.git (push)
4. Copiar el archivo y commitear
5. Pushear master al nuevo remote
Counting objects: 3, done.
Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
To /opt/xfusioncorp_cluster.git
* [new branch] master -> master
Troubleshooting
| Problema | Solucion |
|---|---|
fatal: 'dev_cluster' does not appear to be a git repository |
La ruta del remote no existe o es incorrecta. Verificar con ls /opt/xfusioncorp_cluster.git |
error: remote dev_cluster already exists |
El remote ya fue agregado. Verificar con git remote -v |
git commit -am no incluye el archivo nuevo |
-a solo trackea archivos ya conocidos por Git. Usar git add archivo antes del commit |
error: failed to push some refs |
El remote tiene commits que el local no tiene. Hacer git pull dev_cluster master antes |