Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
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

Bash Asking yes/no questions

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.

Bash A defensive script with fatal errors

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) returns 1 when there are differences; plain git diff always returns 0. git grep returns 1 for "no match", git commit returns 1 when there is nothing to commit, and git merge returns 1 on conflicts.
  • 128 means fatal. Anything that stops with fatal: exits 128 (not a repository, unknown revision, ...). Distinguish it from 1 when "no" is a legitimate answer: git merge-base --is-ancestor gives 1 for "no" and 128 for a bad name.
  • Use machine formats. git status --porcelain, git for-each-ref --format=..., git log --format=..., git rev-list and -z for NUL-separated paths survive file names with spaces and newlines. Never parse git branch or git log default output.
  • set -e is not enough. It ignores failures in conditions and pipelines. Combine it with set -o pipefail and test critical commands explicitly with ||.
  • Locale and color. Set LC_ALL=C and GIT_PAGER=cat (or use git --no-pager) when output is read by a program, and GIT_TERMINAL_PROMPT=0 so 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-msg or pre-push aborts the operation (see Hooks).
  • Network commands retry poorly. git fetch and git push fail with 128 on connection problems; wrap them in your own retry logic.