Módulo 5: GitHub y Colaboración Remota

07. Upstream y Múltiples Remotes

Descripción de la cápsula

En esta cápsula dominarás upstream tracking, trabajar con múltiples remotes, y el workflow de fork/upstream común en open-source. Aprenderás a sincronizar con el repo original después de hacer fork, y gestionar múltiples remotes simultáneamente.


¿Qué es upstream tracking?

Upstream tracking:

Conexión configurada entre branch local y branch remoto para push/pull simplificado.

Con tracking:

git push    # Sin especificar remote/branch
git pull

Sin tracking:

git push origin main  # Necesitas especificar siempre
git pull origin main

Configurar upstream tracking

Método 1: Durante primer push

git push -u origin main

-u configura tracking automáticamente.


Método 2: set-upstream manual

git branch --set-upstream-to=origin/main main

Verificar tracking:

git branch -vv

Output:

* main    c7f9d4b [origin/main] Last commit
  feature d4c6e2b [origin/feature: ahead 2] Feature commit

[origin/main]: main local trackea origin/main.


Múltiples remotes

Escenario común: Fork de proyecto

Original repo (upstream):
github.com/facebook/react

Tu fork (origin):
github.com/tu-usuario/react

Setup de remotes:

# Clonar tu fork
git clone https://github.com/tu-usuario/react.git
cd react

# Ver remote actual
git remote -v
# origin  https://github.com/tu-usuario/react.git

# Agregar upstream (repo original)
git remote add upstream https://github.com/facebook/react.git

# Verificar
git remote -v

Output:

origin    https://github.com/tu-usuario/react.git (fetch)
origin    https://github.com/tu-usuario/react.git (push)
upstream  https://github.com/facebook/react.git (fetch)
upstream  https://github.com/facebook/react.git (push)

Sincronizar fork con upstream

Workflow completo:

# 1. Fetch upstream
git fetch upstream

# 2. Checkout main local
git switch main

# 3. Merge cambios de upstream
git merge upstream/main

# 4. Push a tu fork
git push origin main

Ahora tu fork está actualizado con el original.


Casos de uso de múltiples remotes

Caso 1: Contribuir a open-source

# Setup inicial
git clone https://github.com/tu-usuario/project-fork.git
git remote add upstream https://github.com/original/project.git

# Actualizar antes de trabajar
git fetch upstream
git merge upstream/main

# Trabajar en feature
git switch -c feature/nueva-feature
# ... commits ...

# Push a TU fork
git push origin feature/nueva-feature

# Crear PR desde tu fork al original (GitHub UI)

Caso 2: Múltiples backups

# Remote principal (GitHub)
git remote add origin https://github.com/user/repo.git

# Backup en GitLab
git remote add gitlab https://gitlab.com/user/repo.git

# Push a ambos
git push origin main
git push gitlab main

Caso 3: Deploy remotes

# Remote de desarrollo
git remote add origin https://github.com/user/repo.git

# Remote de producción (Heroku, etc.)
git remote add production https://git.heroku.com/app.git

# Deploy a producción
git push production main

Configurar diferentes URLs para fetch/push

Ejemplo: Fork workflow

# Fetch del original, push a tu fork
git remote set-url --push upstream no-push

# Ahora:
# git fetch upstream  ✅ Funciona
# git push upstream   ❌ Falla (protección)

Ver configuración de remotes

# Ver configuración completa
git remote show origin

Output:

* remote origin
  Fetch URL: https://github.com/user/repo.git
  Push  URL: https://github.com/user/repo.git
  HEAD branch: main
  Remote branches:
    main   tracked
    develop tracked
  Local branches configured for 'git pull':
    main   merges with remote main
  Local refs configured for 'git push':
    main   pushes to main (up to date)

Cambiar URL de remote

Método 1: set-url

git remote set-url origin https://nueva-url.git

Método 2: Eliminar y recrear

git remote remove origin
git remote add origin https://nueva-url.git

Push a remotes específicos

# Push a origin
git push origin main

# Push a upstream (si tienes permisos)
git push upstream main

# Push a ambos
git push origin main && git push backup main

Fetch de múltiples remotes

# Fetch de todos
git fetch --all

# Fetch específico
git fetch upstream
git fetch origin

Workflow de fork completo (ejemplo real)

Paso 1: Fork en GitHub (UI)

  1. Ir a github.com/original/repo
  2. Click "Fork"
  3. Crea fork en tu cuenta

Paso 2: Clonar y configurar

# Clonar tu fork
git clone https://github.com/tu-usuario/repo.git
cd repo

# Agregar upstream
git remote add upstream https://github.com/original/repo.git

# Verificar
git remote -v

Paso 3: Sincronizar con upstream (diario)

# Fetch upstream
git fetch upstream

# Actualizar main
git switch main
git merge upstream/main

# Push a tu fork
git push origin main

Paso 4: Crear feature

# Asegurar main actualizado
git switch main
git pull origin main
git merge upstream/main

# Crear feature branch
git switch -c feature/mi-contribucion

# Trabajar
git commit -m "feat: Mi contribución"

# Push a tu fork
git push origin feature/mi-contribucion

Paso 5: Pull Request (GitHub UI)

  1. Ir a tu fork en GitHub
  2. "Compare & pull request"
  3. Base: original/repo:main
  4. Compare: tu-usuario/repo:feature/mi-contribucion

Troubleshooting común

Problema 1: Push a upstream falla

Síntoma:

git push upstream main
# Permission denied

Causa: No tienes permisos de escritura en upstream.

Solución:

# Solo pusheas a origin (tu fork)
git push origin main

Problema 2: Upstream desactualizado

Causa: Olvidaste fetch.

Solución:

git fetch upstream
git merge upstream/main

Problema 3: Conflictos al merge upstream

Solución:

git fetch upstream
git merge upstream/main
# Resolver conflictos
git add .
git commit

Buenas prácticas

✅ DO: Sync frecuente con upstream

# Cada día en proyectos activos
git fetch upstream
git merge upstream/main

✅ DO: Usa nombres descriptivos para remotes

git remote add company-gitlab https://gitlab.company.com/repo.git
git remote add backup-bitbucket https://bitbucket.org/repo.git

✅ DO: Configura upstream en forks

# Siempre después de fork
git remote add upstream <original-repo-url>

❌ DON'T: Push a upstream sin permisos

# ❌ Generalmente no tienes permisos
git push upstream main

# ✅ Push a origin (tu fork)
git push origin main

Resumen

Configuraste upstream tracking
Trabajaste con múltiples remotes
Sincronizaste forks
Workflow open-source completo

Siguiente: Mini-proyecto de colaboración remota


Ejercicios prácticos

Ejercicio 1: Configurar tracking

git switch -c test-tracking
git push -u origin test-tracking

# Verificar
git branch -vv

Ejercicio 2: Fork workflow (simulado)

# Simular upstream
git remote add upstream https://github.com/facebook/react.git

# Fetch
git fetch upstream

# Ver branches de upstream
git branch -r | grep upstream

Recursos adicionales

  1. Git Remote Documentation
  2. GitHub: Fork & PR Workflow
  3. Atlassian: Multiple Remotes

Tiempo estimado: 20 minutos

Siguiente: 08. Mini-proyecto: Colaboración Completa