Is it possible to have different Git configuration for different projects?

Question

.gitconfig is usually stored in the user.home directory.

I use a different identity to work on projects for Company A and something else for Company B (primarily the name / email). How can I have two different Git configurations so that my check-ins don't go with the name / email?

Answer

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.

At the end of your .gitconfig file, 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 (the [core] section can be omitted if the global ssh key can be used):

[user]
name = John Smith
email = [email protected]

[core] sshCommand = ssh -i ~/.ssh/id_rsa_companya

Example contents of .gitconfig-company_b:

[user]
name = John Smith
email = [email protected]

[core] sshCommand = ssh -i ~/.ssh/id_rsa_companyb

How do I provide a username and password when running "git clone [email protected]"?

Hard reset of a single file