.gitconfig
is usually stored in the user.home
directory.
I use a different identity to work on projects for Company A and something else fo
To be explicit, you can also use --local
to use current repository config file:
git config --local user.name "John Doe"
As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in ~/company_a
directory, and Company B's repos in ~/company_b
.
In your .gitconfig
you can put something like this.
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
Example contents of .gitconfig-company_a
[user]
name = John Smith
email = john.smith@companya.net
Example contents of .gitconfig-company_b
[user]
name = John Smith
email = js@companyb.com
You can also point the environment variable GIT_CONFIG
to a file that git config
should use. With GIT_CONFIG=~/.gitconfig-A git config key value
the specified file gets manipulated.
Another way is to use direnv and to separate config files per directory. For example:
.
├── companyA
│ ├── .envrc
│ └── .gitconfig
├── companyB
│ ├── .envrc
│ └── .gitconfig
└── personal
├── .envrc
└── .gitconfig
Each .envrc
should contain something like this:
export GIT_CONFIG=$(pwd)/.gitconfig
And .gitconfig
is usual gitconfig with desired values.
Thanks @crea1
A small variant:
As it is written on https://git-scm.com/docs/git-config#_includes:
If the pattern ends with
/
,**
will be automatically added. For example, the patternfoo/
becomesfoo/**
. In other words, it matchesfoo
and everything inside, recursively.
So I use in my case,
~/.gitconfig :
[user] # as default, personal needs
email = myalias@personal-domain.fr
name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
path = .gitconfig-job
# all others section: core, alias, log…
So If the project directory is in my ~/wokspace/
, default user settings is replace with
~/.gitconfig-job :
[user]
name = John Smith
email = js@company.com
Find .gitconfig from the system
File Location For Windows : "C:\Users${USER_NAME}.gitconfig"
File Location For Linux : "/usr/local/git/etc/gitconfig"
Open .gitconfig file and add below lines as per your condition
[includeIf "gitdir:D:\ORG-A-PROJECTS\"]
[user]
name = John Smith
email = js@organizationx.com [includeIf "gitdir:~/organization_b/"]
[user]
name = John Doe
email = jd@organizationy.com