Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Environment Variables

GIT_CONFIG_* environment variables

Reference for GIT_CONFIG_COUNT/KEY/VALUE, GIT_CONFIG_GLOBAL, GIT_CONFIG_SYSTEM and GIT_CONFIG_NOSYSTEM: inject or replace Git configuration without touching files.

Git reads configuration from files: system, global (~/.gitconfig), local (.git/config) and per-worktree. A family of GIT_CONFIG_* environment variables lets a script or test harness change that picture for one process tree without editing any file.

There are two different jobs here. GIT_CONFIG_COUNT with GIT_CONFIG_KEY_<n> and GIT_CONFIG_VALUE_<n> adds settings that outrank the files, like git -c but inherited by child processes. GIT_CONFIG_GLOBAL, GIT_CONFIG_SYSTEM and GIT_CONFIG_NOSYSTEM replace or remove whole files, which is how you get a reproducible, user-independent Git.

Terminal
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.pager GIT_CONFIG_VALUE_0=cat git log
GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 git config --list --show-origin

Try it

Bash Inject settings that beat every file

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 Replace the global file

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.

Syntax

Terminal
GIT_CONFIG_COUNT=<n> GIT_CONFIG_KEY_0=<key> GIT_CONFIG_VALUE_0=<value> ... git <command>
GIT_CONFIG_GLOBAL=<path>   git <command>
GIT_CONFIG_SYSTEM=<path>   git <command>
GIT_CONFIG_NOSYSTEM=1      git <command>
Name Description
GIT_CONFIG_COUNT How many key/value pairs follow. Numbering starts at 0 and has no gaps.
GIT_CONFIG_KEY_<n> The setting name, for example core.pager or url.git@host:.insteadOf.
GIT_CONFIG_VALUE_<n> Its value. Empty values are allowed.
GIT_CONFIG_GLOBAL Path of the file to use instead of ~/.gitconfig and $XDG_CONFIG_HOME/git/config.
GIT_CONFIG_SYSTEM Path of the file to use instead of the system file (/etc/gitconfig).
GIT_CONFIG_NOSYSTEM If set to a true value, the system file is skipped entirely.

Edge cases: if GIT_CONFIG_COUNT is set but a key or value for an index below it is missing, Git stops with an error rather than guessing. Keys must be in section.name form. Pointing GIT_CONFIG_GLOBAL at /dev/null (or an empty file) gives an empty global configuration. git config --global follows the replaced path, so writes go to the file you named.

Usage notes

  • Precedence: command-line -c and the GIT_CONFIG_* pairs sit above all files. Use them for settings that must win over whatever the user has configured.
  • Child processes inherit them. Unlike git -c, the pairs survive into hooks, aliases, submodule commands and scripts started by Git. Git itself passes -c settings to children through the internal GIT_CONFIG_PARAMETERS variable; do not set that one by hand.
  • Hermetic tests: HOME=$(mktemp -d) GIT_CONFIG_NOSYSTEM=1 (or the GLOBAL/SYSTEM pair pointed at /dev/null) isolates a test run from the developer's settings.
  • CI secrets: injecting http.extraHeader through the environment keeps a token out of .git/config and out of the process list that git -c would expose.
  • Multi-valued keys such as credential.helper can be given by repeating the same key at different indexes.
  • GIT_CONFIG (without a suffix) is not a general override; only git config itself honors it, and only for reading or writing a single file. Prefer git config --file <path>.
  • Introduced in Git 2.31 (GIT_CONFIG_COUNT) and 2.32 (GIT_CONFIG_GLOBAL, GIT_CONFIG_SYSTEM).