Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-config

.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?

like image 439
priya Avatar asked Jan 10 '12 10:01

priya


People also ask

Can you have multiple git configs?

With conditional includes in Git 2.13, it is now possible to have multiple user/email coexist on one machine with little work.

Can I change git config?

The global git config is simply a text file, so it can be edited with whatever text editor you choose. Open, edit global git config, save and close, and the changes will take effect the next time you issue a git command. It's that easy.

Is git config local or global?

System vs Global vs Local Git config System Git config controls settings for all users and all repositories on your computer. Global Git config controls settings for the currently logged in user and all his repositories. Local Git config controls settings for a specific repository.

How do I apply a configuration across the entire git environment?

Git stores all global configurations in . gitconfig file, which is located in your home directory. To set these configuration values as global, add the --global option, and if you omit --global option, then your configurations are specific for the current Git repository. You can also set up system wide configuration.


2 Answers

There are 3 levels of git config; project, global and system.

  • project: Project configs are only available for the current project and stored in .git/config in the project's directory.
  • global: Global configs are available for all projects for the current user and stored in ~/.gitconfig.
  • system: System configs are available for all the users/projects and stored in /etc/gitconfig.

Create a project specific config, you have to execute this under the project's directory:

$ git config user.name "John Doe"  

Create a global config:

$ git config --global user.name "John Doe" 

Create a system config:

$ git config --system user.name "John Doe"  

And as you may guess, project overrides global and global overrides system.

Note: Project configs are local to just one particular copy/clone of this particular repo, and need to be reapplied if the repo is recloned clean from the remote. It changes a local file that is not sent to the remote with a commit/push.

like image 125
teymourlouie Avatar answered Sep 18 '22 15:09

teymourlouie


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 (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 
like image 43
crea1 Avatar answered Sep 19 '22 15:09

crea1