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

Sinopsis

git apply [--check] [--index] [--3way] <patch>...

Descripción

El comando git apply aplica un archivo de patch (en formato diff unificado) al árbol de trabajo, al index, o a ambos. Es como patch(1) pero entiende los formatos de Git, incluyendo cambios de modos de archivo, renames y submódulos.

A diferencia de git am, no crea commits: solo modifica archivos. Se usa cuando recibes un patch de un archivo, email o herramienta y quieres aplicarlo sin que afecte el historial de commits.

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

OpciónDescripción
--checkTest if the patch applies without modifying anything.
--indexApply to working tree and index.
--cachedApply to index only.
--3wayThree-way merge if context doesn't match.
--reverse / -RApply in reverse (uncommit a patch).
--whitespace=<action>Handle whitespace errors (warn, fix, error).
-p<n>Strip n path components from filenames.

Ejemplos

git apply changes.patch
# Apply a patch file to the working tree

git apply --check feature.patch
# Verify a patch will apply cleanly

git apply --index --3way mr.diff
# Apply with three-way merge fallback, into index too

curl -L https://example.com/fix.patch | git apply
# Pipe a patch directly from a URL

Errores comunes

If the patch was generated against a different base, plain git apply fails with "patch does not apply." Adding --3way often saves the day by attempting a merge. For patches that include commit metadata (author, message), use git am instead so the commit is recreated faithfully.

Comandos relacionados

git am, git format-patch, git diff, git cherry-pick