Is there a way to change git's default configuration file?

89 Views Asked by At

I see git allows me to choose any file I want as a global exclude file but it can't see that you can change the default global configuration file ?

Did I miss it or is it impossible? Are there workarounds ?

1

There are 1 best solutions below

0
ElpieKay On BEST ANSWER

Git has 2 kinds of global configuration files. One is ~/.gitconfig and the other is $XDG_CONFIG_HOME/git/config.

When ~/.gitconfig does not exist, $XDG_CONFIG_HOME/git/config works as the global configuration file.

We can create multiple git/config under different paths, for example /e/git/config and /f/git/config.

mkdir -p /e/git
touch /e/git/config
mkdir -p /f/git
touch /f/git/config

When we want to use one of them as the global configuration file, first we rename ~/.gitconfig.

mv ~/.gitconfig ~/.gitconfig.bak

And then assign a path to XDG_CONFIG_HOME with either export XDG_CONFIG_HOME= or XDG_CONFIG_HOME=,

# use /e/git/config
export XDG_CONFIG_HOME=/e
git config --global user.name foo
git config --global user.email [email protected]

# use /f/git/config
XDG_CONFIG_HOME=/f git config --global user.name bar
XDG_CONFIG_HOME=/f git config --global user.email [email protected]

To use different names and emails,

git init test
cd test
touch a.txt
git add a.txt
# use foo and [email protected]
export XDG_CONFIG_HOME=/e
git commit -m'hello foo'
# disable XDG_CONFIG_HOME
unset XDG_CONFIG_HOME

touch b.txt
git add b.txt
# use bar and [email protected]
XDG_CONFIG_HOME=/f git commit -m"hello bar"

When we want to use ~/.gitconfig again,

mv ~/.gitconfig.bak ~/.gitconfig