Git Reference ยท Concepts
Exit codes and scripting with Git
Use Git's exit codes in shell scripts: which commands answer yes/no through their status, how to detect dirty trees, and how to script safely with set -e.
Like any Unix tool, Git reports success with exit status 0 and failure with a non-zero status. Many commands go further and use the status as the answer to a question, which lets a script ask Git things without parsing text. The usual non-zero values are 1 (the answer is "no", or a differences/conflict condition) and 128 (a fatal error such as "not a git repository" or a failed push precondition).
Reliable scripts rely on three habits: use statuses and plumbing instead of scraping human output, quote everything, and decide explicitly what happens when a command fails.
git diff --quiet # 0: no unstaged changes, 1: changes git diff --cached --quiet # same for the index git merge-base --is-ancestor A B # 0: A is an ancestor of B git rev-parse --verify -q refs/heads/x # 0 and prints the id if the ref exists git cat-file -e "$sha^{commit}" # 0 if that commit exists
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Usage notes
- Exit codes are not uniform.
git diff --exit-code(and--quiet) returns1when there are differences; plaingit diffalways returns0.git grepreturns1for "no match",git commitreturns1when there is nothing to commit, andgit mergereturns1on conflicts. 128means fatal. Anything that stops withfatal:exits 128 (not a repository, unknown revision, ...). Distinguish it from1when "no" is a legitimate answer:git merge-base --is-ancestorgives1for "no" and128for a bad name.- Use machine formats.
git status --porcelain,git for-each-ref --format=...,git log --format=...,git rev-listand-zfor NUL-separated paths survive file names with spaces and newlines. Never parsegit branchorgit logdefault output. set -eis not enough. It ignores failures in conditions and pipelines. Combine it withset -o pipefailand test critical commands explicitly with||.- Locale and color. Set
LC_ALL=CandGIT_PAGER=cat(or usegit --no-pager) when output is read by a program, andGIT_TERMINAL_PROMPT=0so a missing credential fails instead of hanging. - Pass revisions safely. Put
--before file names and use--end-of-options(Git 2.24+) before user-supplied revision names, so a name starting with-is not read as an option. - Capture the id, not the name.
sha=$(git rev-parse --verify "$name^{commit}")pins a revision even if branches move while the script runs. - Hooks use status too. A non-zero exit from
pre-commit,commit-msgorpre-pushaborts the operation (see Hooks). - Network commands retry poorly.
git fetchandgit pushfail with 128 on connection problems; wrap them in your own retry logic.