I want to load a specific .gitconfig
only for the current session.
I tried
git config -f ~/path/to/my/.gitconfig
but it only answers by the man page:
usage: git config [<options>]
Config file location
--global use global config file
--system use system config file
--local use repository config file
etc.
I also tried
GIT_CONFIG=/path/to/my/.gitconfig
but it is not taken into account.
EDIT: In this question, "current session" means "current bash session".
CodePudding user response:
Starting from git version 2.32.0, you can set the environment variable GIT_CONFIG_GLOBAL
in order to redirect to a different file than ~/.gitconfig. This version also introduces the variable GIT_CONFIG_SYSTEM
, which would override /etc/gitconfig
.
With git versions before, you can kind of simulate the same behavior, by unsetting $HOME, and set $XDG_CONFIG_HOME to a directory which contains a git folder with a config file residing in it. But I would advice against touching $HOME, since this will also affecting any program git runs itself. Also the git developers made it extremely hard to skip any $HOME/.gitconfig file, so there is not really much you could do if your are stuck with an older version.
~/.gitconfig
[alias]
foo = !foo
different-config
[alias]
foo = !bar
In an interactive session you can see the difference:
$ git help foo
'foo' is aliased to '!foo'
$ GIT_CONFIG_GLOBAL=/path/to/different-config git help foo
'foo' is aliased to '!bar'
So I would suggest to set GIT_CONFIG_GLOBAL
to the desired file, which then will get read instead of ~/.gitconfig. Note that a .git/config
will still be read, and everything in .git/config
will take precedence over the GIT_CONFIG_GLOBAL
file.
CodePudding user response:
git config
without any action just prints the help page you see. Do something with the config file, for example
git config -f ~/path/to/my/.gitconfig --list
- Try
export GIT_CONFIG
to use the environment variables in subprocesses. Not exported environment variables are available only to the current shell but not in subprocesses.