5. Trabajo con GitHub

En este documento se explica cómo interactuar con GitHub, desde conectar un repositorio local hasta colaborar mediante Pull Requests, Issues y otras herramientas avanzadas.

1. Conectar un Repositorio Local con GitHub

1.1. Crear un Repositorio en GitHub

1.2. Vincular el Repositorio Local (git remote add)

Ejecuta estos comandos en tu repositorio local:

git remote add origin https://github.com/usuario/repositorio.git  # HTTPS
git remote add origin git@github.com:usuario/repositorio.git     # SSH

Verificar conexión:

git remote -v  # Muestra las URLs configuradas

2. Subir y Bajar Cambios

2.1. git push – Subir Cambios a GitHub

Envía commits locales a la rama remota:

git push -u origin main  # Primera vez (establece upstream)
git push                 # Subidas posteriores

2.2. git pull – Descargar Cambios del Remoto

Combina git fetch + git merge para actualizar tu repositorio local:

git pull origin main

2.3. git fetch – Traer Cambios sin Fusionar

Descarga cambios del remoto pero no los aplica:

git fetch origin  # Trae cambios
git diff main origin/main  # Compara con la rama remota

3. Autenticación en GitHub

3.1. HTTPS vs SSH

Método Ventajas Desventajas
HTTPS Fácil de configurar. Requiere token o credenciales cada vez.
SSH No pide credenciales frecuentemente. Configuración inicial más compleja.

Generar Clave SSH

Ejecuta en tu terminal:

ssh-keygen -t ed25519 -C "tu@email.com"

Agrega la clave pública (~/.ssh/id_ed25519.pub) en GitHub SSH Keys.

3.2. Tokens de Acceso Personal (PAT)

Reemplazan contraseñas en HTTPS:

4. Forks y Pull Requests (PRs)

4.1. Fork – Copiar un Repositorio Ajeno

git clone https://github.com/tu-usuario/repo.git

4.2. Crear un Pull Request

  1. Crea una rama para tus cambios:
git checkout -b mi-feature
  1. Sube los cambios a tu fork:
git push origin mi-feature
  1. En GitHub, ve a Pull Requests > New PR y selecciona:
    • Base repository: Original (original/repo).
    • Head repository: Tu fork (tu-usuario/repo:mi-feature).

4.3. Revisión de Código

5. Issues y Gestión de Tareas

5.1. Crear un Issue

5.2. Referenciar Issues en Commits

Cierra automáticamente un issue con palabras clave en el commit:

git commit -m "fix: corregir error en login. Close #123"

6. Herramientas Avanzadas de GitHub

6.1. GitHub Projects

Tablero Kanban para organizar tareas:

6.2. GitHub Actions (CI/CD)

Automatiza pruebas, despliegues y más con workflows en .github/workflows/:

name: Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm install && npm test

6.3. Otras Herramientas

7. Diagrama de Flujo GitHub

graph TB
    A[Repositorio Local] -->|git push| B[GitHub]
    B -->|git pull| A
    C[Fork] -->|Pull Request| B
    B -->|Review| D[Merge]

8. Resumen de Comandos

Comando Uso
git remote add origin Vincula un repositorio local con GitHub.
git push -u origin main Sube cambios y establece rama upstream.
git pull Descarga y fusiona cambios.
git fetch Trae cambios sin fusionar.

9. Conclusión

Siguiente: 6. Ramas Avanzadas y Git Flow


Ejercicios

5.1 — Crea un repositorio vacío en GitHub (sin README). Conéctalo con un repositorio local existente y sube todos los commits con git push -u origin main.

5.2 — Haz fork del repositorio https://github.com/firstcontributions/first-contributions. Clona tu fork, crea una rama feature/tu-nombre, añade tu nombre al fichero indicado y abre un Pull Request al repositorio original.

5.3 — En un repositorio propio, crea un Issue titulado “Añadir página de contacto”. Luego haz un commit que lo cierre automáticamente usando la palabra clave Closes #1 en el mensaje.

5.4 — Configura autenticación SSH para GitHub: genera una clave ed25519, añade la clave pública a tu cuenta de GitHub y verifica la conexión con ssh -T git@github.com.

Soluciones

5.1

# En GitHub: New repository → sin README, sin .gitignore

# En local
cd mi-proyecto
git remote add origin https://github.com/tu-usuario/mi-proyecto.git
git branch -M main
git push -u origin main

# Verificar
git remote -v
# origin  https://github.com/tu-usuario/mi-proyecto.git (fetch)
# origin  https://github.com/tu-usuario/mi-proyecto.git (push)

5.2

# 1. Fork en GitHub (botón Fork en la página del repo)

# 2. Clonar tu fork
git clone https://github.com/tu-usuario/first-contributions.git
cd first-contributions

# 3. Crear rama y hacer cambio
git checkout -b feature/tu-nombre
# Editar Contributors.md añadiendo tu nombre
git add Contributors.md
git commit -m "feat: add Tu Nombre to contributors"

# 4. Subir rama a tu fork
git push origin feature/tu-nombre

# 5. En GitHub: Compare & pull request → base: firstcontributions/first-contributions

5.3

# En GitHub: Issues → New issue → "Añadir página de contacto" → Submit

# En local
echo "<section>Contacto</section>" > contacto.html
git add contacto.html
git commit -m "feat: add contact page. Closes #1"
git push

# El Issue #1 se cerrará automáticamente al hacer push a la rama principal

5.4

# 1. Generar clave
ssh-keygen -t ed25519 -C "tu@email.com"
# Acepta la ruta por defecto (~/.ssh/id_ed25519)

# 2. Copiar clave pública
cat ~/.ssh/id_ed25519.pub
# Copia el resultado

# 3. En GitHub: Settings → SSH and GPG keys → New SSH key → pega la clave

# 4. Verificar
ssh -T git@github.com
# Hi tu-usuario! You've successfully authenticated, but GitHub does not provide shell access.

# 5. Cambiar remote a SSH
git remote set-url origin git@github.com:tu-usuario/mi-proyecto.git