12

I'm in a bit of a bind with Git. I'm trying to execute git commit but I need to be able to swtich between ~/.gitconfig1 and ~/.gitconfig2 Is there a command line switch - or anyway to have Git use a different gitconfig file then the ones found at /etc/gitconfig, ~/.gitconfig and .git/config?

Marco Ceppi
  • 7,163
  • 5
  • 31
  • 43
  • Do you actually need two different configs while in one repo? Can you just use the .git/config for each repo to get what you want? I guess it's just not very elegant if you have only two common settings to spread across dozens of repos? – Cascabel Aug 29 '10 at 04:24
  • @Jefromi Yes I do. It's a unique situation that I have come across where two configs are needed – Marco Ceppi Aug 29 '10 at 15:03
  • 2
    The other obvious alternative would be to globally alias `git commit-v2` (or something shorter) to `!export GIT_AUTHOR_NAME="Second name"; git commit`. – Cascabel Aug 29 '10 at 15:43
  • @Torek - More git goodness. These developers should be real proud of their inabilities.... – jww Sep 18 '16 at 04:09
  • 1
    Did you try `includeIf`, see https://git-scm.com/docs/git-config – amirouche Mar 04 '22 at 17:32

4 Answers4

16

I found a way to execute this - it wasn't elegant but it did work - and so far seems to be the only way to get this to work.

Git uses the HOME path to determine where .gitconfig is. I was able to perform something like this:

/home/marco/.silly/.gitconfig
/home/marco/.stupid/.gitconfig
/home/marco/.gitconfig

And when executing Git Commit (which is the only command that requires the .gitconfig) I override the home path.

HOME=/home/marco/.silly/ git commit -m "silly configuration"

You can then use alias to do this easily

alias sillygit="HOME=/home/marco/.silly/ git"
sillygit commit -m "silly stuff"
alper
  • 2,919
  • 9
  • 53
  • 102
Marco Ceppi
  • 7,163
  • 5
  • 31
  • 43
  • 1
    Nice hack. I can't seem to find any real way to do this - there's a GIT_CONFIG environment variable, but it only affects `git config` itself, in the same way as its `-f,--file` option. – Cascabel Aug 29 '10 at 15:38
  • For the git version 2.35.2 and above first user have to apply: `env HOME=/home/marco/.silly git config --global --add safe.directory ` – alper Apr 13 '22 at 22:15
3

Mario Ceppi's alias approach can be used in a slightly more elegant way using the -c config=value argument to git:

$ alias sillygit="git -c user.name=Silly -c user.email=silly@silly.org"
$ sillygit commit

This of course assumes you don't mind keeping the differing config keys in your .bashrc or the like instead of in your .gitconfig, and it has the caveat of breaking shell completion.

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
0

@amirouche's comment and Emil Lundberg's answer can be combined to actually load another Git config file:

alias git="git -c 'include.path=/some/path/to/my/custom/.gitconfig'"

However, the user's ~/.gitconfig file is still loaded. This approach only overwrites settings from the other git config files.

IngoMeyer
  • 303
  • 4
  • 7
-3

You can use --git-dir

git --git-dir /home/marco/silly/.git commit ...
dldnh
  • 8,923
  • 3
  • 40
  • 52