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.
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
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.
Syntax
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
-cand theGIT_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-csettings to children through the internalGIT_CONFIG_PARAMETERSvariable; do not set that one by hand. - Hermetic tests:
HOME=$(mktemp -d) GIT_CONFIG_NOSYSTEM=1(or theGLOBAL/SYSTEMpair pointed at/dev/null) isolates a test run from the developer's settings. - CI secrets: injecting
http.extraHeaderthrough the environment keeps a token out of.git/configand out of the process list thatgit -cwould expose. - Multi-valued keys such as
credential.helpercan be given by repeating the same key at different indexes. GIT_CONFIG(without a suffix) is not a general override; onlygit configitself honors it, and only for reading or writing a single file. Prefergit config --file <path>.- Introduced in Git 2.31 (
GIT_CONFIG_COUNT) and 2.32 (GIT_CONFIG_GLOBAL,GIT_CONFIG_SYSTEM).