Sinopsis
git rev-parse [<options>] <arg>...
Descripción
El comando git rev-parse es el navaja suiza de plumbing para resolver nombres de revisión a SHAs. Toma cualquier referencia (HEAD, branch, tag, expresión como HEAD~3) y devuelve el SHA correspondiente.
También responde preguntas estructurales: --git-dir, --show-toplevel, --is-inside-work-tree. Esencial para scripts que necesitan ubicar archivos relativos al repo.
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.
Cuándo usar
You'll see git rev-parse in nearly every shell script that wraps Git: to get the current branch name, the repo root, or to validate that an argument is a valid commit. Most users don't run it directly day-to-day, but knowledge of it dramatically improves Git scripting fluency.
Opciones comunes
| Opción | Descripción |
|---|---|
--abbrev-ref | Print short ref name (e.g. main, not refs/heads/main). |
--short | Output abbreviated SHA. |
--show-toplevel | Print absolute path of repo root. |
--git-dir | Print absolute path of .git. |
--is-inside-work-tree | Yes/no script-friendly answer. |
--verify | Validate that the argument is a real ref. |
--quiet | Suppress error output. |
Ejemplos
git rev-parse HEAD
# Full SHA of current commit
git rev-parse --abbrev-ref HEAD
# Current branch name (or "HEAD" if detached)
git rev-parse --show-toplevel
# Path to repo root, useful in scripts
git rev-parse --verify origin/main >/dev/null && echo OK
# Test whether a ref exists
Errores comunes
Using git rev-parse on user input without --verify can succeed for ambiguous arguments and produce surprising results. In subshells, --show-toplevel is a more reliable anchor than $PWD.
Comandos relacionados
git symbolic-ref, git rev-list, git for-each-ref, git config