Por Anónimo (no verificado) , 29 Abril 2026

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 commandDescription
yStage this hunk.
nDon't stage this hunk.
sSplit into smaller hunks.
eManually edit the hunk.
aStage this and all remaining hunks in this file.
dSkip this and all remaining hunks in this file.
qQuit; 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