Sinopsis
git add -p [<path>...]
git add --patch [<path>...]
Descripción
git add -p ('patch mode') presenta cada hunk de cambios y te pregunta si stagearlo, dándote control fino sobre lo que va al próximo commit. Es la herramienta esencial para crear commits limpios y atómicos cuando tienes cambios mezclados.
Las acciones disponibles incluyen y/n (sí/no), s (split: divide el hunk en pedazos más pequeños), e (edit: edita el hunk a mano) y ? (ayuda). Domínalo para hacer commits significativamente mejores.
En el uso diario, este comando se integra estrechamente con alias de shell, plugins de editor e integración continua. Los usuarios avanzados a menudo añaden alias que combinan los flags que siempre pasan. El formato de salida puede personalizarse vía configuración de Git. Cuando algo sale mal, ejecuta el comando con GIT_TRACE=1 para revelar las llamadas plumbing subyacentes.
Entender cómo este comando interactúa con el resto del modelo de datos de Git rinde dividendos. Cada comando opera sobre algún subconjunto de las piezas (objetos, index, refs, árbol de trabajo), y saber cuáles toca ayuda a predecir resultados y a recuperarse de errores.
Opciones comunes
| Interactive command | Description |
|---|---|
y | Stage this hunk. |
n | Don't stage this hunk. |
s | Split into smaller hunks. |
e | Manually edit the hunk. |
a | Stage this and all remaining hunks in this file. |
d | Skip this and all remaining hunks in this file. |
q | Quit; don't stage further hunks. |
? | Show help. |
Ejemplos
git add -p
# Walk through every changed file's hunks
git add -p src/auth.go
# Restrict to one file
# Inside the prompt:
# Press 's' to split a large hunk into smaller ones
# Press 'e' to manually edit the hunk text
# Press 'y' or 'n' to accept or skip
git diff --cached
# Review what's now staged
Errores comunes
Editing a hunk with e requires understanding the unified diff format — adding a line means the new file has it, removing a context line means the new file lacks it. Mistakes here can produce confusing index state. Always verify with git diff --cached after.
Comandos relacionados
git add, git stash -p, git restore -p, git checkout -p