Shell variable expansion in git config

You can't. git-config(1) does not support environment variable expansion, but only limited type conversion and path expansion:

The type specifier can be either --int or --bool, to make git config ensure that the variable(s) are of the given type and convert the value to the canonical form (simple decimal number for int, a "true" or "false" string for bool), or --path, which does some path expansion (see --path below). If no type specifier is passed, no checks or transformations are performed on the value.

The documentation for --path states:

--path

git-config will expand leading ~ to the value of $HOME, and ~user to the home directory for the specified user. This option has no effect when setting the value (but you can use git config bla ~/ from the command line to let your shell do the expansion).

The term "expansion" does not appear in any different context in git-config(1). So how did you even get the idea that it should, given that no such feature is documented anywhere?

In order to expand environment variables you have to pre-process the Git config file yourself, i.e. by creating a template file, and expand variables with a script before copying the file to your $HOME directory.

If it's about dotfile management, then do, what all people do: Put them in a directory, and add symlinks to this directory from your $HOME.


I use bash scripts in my config to enable variable expansion. Just export the variable you need in your .bashrc and use it in the scripts:

In my ~/.bashrc:

export TESTVARIABLE="hello"

In my ~/.gitconfig:

[alias]
    test = !bash -c '"echo \"Value: $TESTVARIABLE\";"'

At my bash prompt:

bash> git test
    Value: hello 

The other way of doing it, is to add a git config command in your shell's rc. I for instance have in my .bashrc:

git config --global user.name "$USER@$HOSTNAME"

I have the same configuration on all my machines, and by adding this I can distinguish between commits from different machines. You could do the same and add to your shell rc:

export RC="$HOME/rc"
git config --global core.excludesfile "$RC/globalgitignore" 

Tags:

Shell

Git