Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotIf or Else conditional gitconfig

Tags:

git

git-config

Following this SO post and the linked guidance, I set up a condition configuration like the following...

$ cat ~/.gitconfig 
[user]
    name = Random J. Hacker
    email = [email protected]
[includeIf "gitdir:~/work/*"]
    path = ~/work/.gitconfig

$ cat ~/work/.gitconfig 
[user]
    name = Serious Q. Programmer
    email = [email protected]

In this way, all of the repositories I do clone when I'm working for Programmer Co. get my @programmer.biz work email address assigned as the default --author credential; and every random repo I clone to my desktop or /tmp or ~/.vscode or wherever gets my super legit @hack.er credential assigned as the author.

Unfortunately, I noticed the following behaviour...

$ git clone [email protected]:programmerbiz/repo.git
$ cd repo/
$ git config --list | grep user
user.name=Random J. Hacker
[email protected]
user.name=Serious Q. Programmer
[email protected]

Oh no! My @hack.er user is picked up by the business repo!

I would like to automatically configure one and only one default [user] for all repos without resorting to a bash script. Is there an includeIf.Not operator, [else] block syntax, or similar that I can use in ~/.gitconfig to achieve this? The conditional includes documentation does not appear to support this use case.

like image 828
Peter Vandivier Avatar asked Jul 30 '19 15:07

Peter Vandivier


2 Answers

What is the output of git config --list --show-origin | grep user?
You should see where the configurations are defined.

I would like to automatically configure one and only one default [user] for all repos

In this case, remove all the above users configuration and set a single entry in your global config

# Use the `--global` to store it under your user account configuration
# and it will be used as the default for all of your projects unless you
# define it in your local `.git/config`

git config --global user.name <....>
git config --global user.email <....>

---- 
# For specific project use the `--local` flag (will be stored in your `.git/config`

git config --local user.name <....>
git config --local user.email <....>
like image 65
CodeWizard Avatar answered Oct 21 '22 10:10

CodeWizard


The problem is the way you are specifying the gitdir, I had the same issue, and the path (full or relative) should end with a slash.

You have gitdir:~/work/*

This is how it should look:

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

You can then go to the directory and check if it worked:

cd ~/work
git config --list | grep user

Cheers!

like image 2
Willemoes Avatar answered Oct 21 '22 11:10

Willemoes