Ejercicios de Git - Prueba Práctica

1 min

Tabla de contenidos

  1. Ejercicio 1: lo básico
  2. Ejercicio 2: ramas
  3. Ejercicio 3: merge frente a rebase
  4. Ejercicio 4: stash y cherry-pick
  5. Ejercicio 5: urgencia y reset

Ejercicio 1: lo básico

Misión: crear un proyecto, hacer 3 commits y corregir el último.

bash
mkdir test-git && cd test-git && git init

# 3 commits
echo "print('v1')" > app.py && git add . && git commit -m "Version 1"
echo "print('v2')" > app.py && git add . && git commit -m "Version 2"  
echo "print('v3 with bug')" > app.py && git add . && git commit -m "Version 3"

# Corregir el último commit
echo "print('v3 fixed')" > app.py && git add . && git commit --amend -m "Version 3 - Fixed"

Comprobación: git log --oneline → 3 commits; el último dice "Fixed"

Volver a la tabla de contenidos

Ejercicio 2: ramas

Misión: 2 features en paralelo, fusionar con limpieza.

bash
# Base
echo "print('main')" > main.py && git add . && git commit -m "Main app"

# Feature 1  
git checkout -b feature-login
echo "print('login')" > login.py && git add . && git commit -m "Add login"

# Feature 2
git checkout main && git checkout -b feature-api
echo "print('api')" > api.py && git add . && git commit -m "Add API"

# Fusionar todo en main
git checkout main
git merge feature-login
git merge feature-api

# Limpiar
git branch -d feature-login feature-api

Comprobación: git log --graph --oneline → ver los merges

Volver a la tabla de contenidos

Ejercicio 3: merge frente a rebase

Misión: el mismo escenario, 2 estrategias distintas.

Estrategia A: merge

bash
mkdir test-merge && cd test-merge && git init
echo "print('base')" > app.py && git add . && git commit -m "Base"

# Feature branch
git checkout -b feature
echo "print('feature')" > feature.py && git add . && git commit -m "Add feature"

# Main avanza
git checkout main
echo "print('base v2')" > app.py && git add . && git commit -m "Update base"

# Merge
git merge feature
git log --graph --oneline    # Ver el historial con merge

Estrategia B: rebase

bash
mkdir test-rebase && cd test-rebase && git init
echo "print('base')" > app.py && git add . && git commit -m "Base"

git checkout -b feature  
echo "print('feature')" > feature.py && git add . && git commit -m "Add feature"

git checkout main
echo "print('base v2')" > app.py && git add . && git commit -m "Update base"

# Rebase
git checkout feature && git rebase main
git checkout main && git merge feature
git log --oneline           # Historial lineal

Pregunta: ¿qué diferencia ves en git log?

Volver a la tabla de contenidos

Ejercicio 4: stash y cherry-pick

Misión: hacer malabares con varias tareas.

bash
mkdir test-stash && cd test-stash && git init
echo "print('v1')" > app.py && git add . && git commit -m "Version 1"

# Empiezas a programar
echo "print('work in progress')" >> app.py

# ¡URGENCIA! Tienes que cambiar de rama
git stash                    # Guardar
git checkout -b hotfix
echo "print('URGENT FIX')" > fix.py && git add . && git commit -m "Critical fix"

# Cherry-pick del arreglo hacia main
git checkout main
git cherry-pick <hash-du-fix>

# Volver a tu trabajo
git stash pop
echo "print('feature complete')" >> app.py && git add . && git commit -m "Feature done"

Reto: ¿el arreglo urgente está en main Y en hotfix?

Volver a la tabla de contenidos

Ejercicio 5: urgencia y reset

Misión: simular una urgencia con reset.

bash
mkdir test-urgence && cd test-urgence && git init

# Varios commits
echo "print('v1')" > app.py && git add . && git commit -m "Version 1"
echo "print('v2')" > app.py && git add . && git commit -m "Version 2"  
echo "print('v3')" > app.py && git add . && git commit -m "Version 3"
echo "print('v4 BROKEN')" > app.py && git add . && git commit -m "Version 4 - Bug!"

# ¡Oh no! v4 lo rompe todo en producción
git log --oneline                    # Anotar el hash de v3

# Solución 1: reset hard (destructivo)
git reset --hard <hash-v3>           # Vuelta a v3, v4 desaparece
git log --oneline                    # Solo 3 commits

# Solución 2: revert (seguro)  
# Volver a poner v4 primero
git reset --hard <hash-v4>           # Volver a v4
git revert HEAD                      # Crear un commit que anula v4
git log --oneline                    # 5 commits: v1,v2,v3,v4,revert-v4

Lección: revert = seguro en producción; reset = OK en local

Volver a la tabla de contenidos


RETO FINAL

¿Puedes hacer todo esto en 15 minutos?

  1. Proyecto con 3 commits
  2. 2 ramas con features
  3. 1 merge + 1 rebase
  4. 1 stash + 1 cherry-pick
  5. 1 reset o revert de urgencia

Si sí → ¡dominas Git!

Volver a la tabla de contenidos