How to set up Git User for company and personal projects respectively?

Original link:


From the first day using Git, everyone can’t escape this configuration:

 git config --global Name git config --global Email

Directly set a global user for Git, or remove --global when there is only one project, and just execute the command to set the user in the project directory.

So here comes the problem. When there are multiple company projects and personal projects in the computer, using the global default configuration may accidentally be nailed to the pillar of shame in the Git history of the company project: Imagine that future generations will see it. After getting to this code, I was really confused. I looked at who wrote it and found out that it was a company email. I may not know you yet. If you use QQ email…

The global user cannot handle the mixed use problem friendlyly on a computer where personal projects and company projects coexist, and it still needs to be set up separately in each project.

how to solve this problem?

Use includeIf

includeIf is a feature in the Git configuration system that allows you to automatically include a set of configuration settings when certain conditions are met. This feature is particularly useful for handling different work/project environments on a single machine.

The prerequisite for use is that you need to have a simple plan for the design of the folder. For example:

My way is to create two directories under the user root directory ~ .

  • GitHub Only personal projects and open source projects are placed in this directory
  • workspace This directory stores company projects

Create another gitconfig directory in the root directory to store some configuration files. You can also add version management to facilitate migration.

 mkdir ~/gitconfig && cd $_

Go into the directory and create the following files


 [user] name = lufei email = [email protected]


 [user] name = sy-records email = [email protected]


 .idea .vscode .env vendor/ composer.lock test.php node_modules/ .DS_Store


 [user] name = sy-records email = [email protected] [core] excludesfile = ~/gitconfig/.gitignore_global [includeIf "gitdir:~/GitHub/"] path = ~/gitconfig/.gitconfig_individual [includeIf "gitdir:~/workspace/"] path = ~/gitconfig/.gitconfig_company

Explain the contents of this file, looking from bottom to top:

  • Projects in the workspace directory use company-related configurations in the ~/gitconfig/.gitconfig_company file;
  • Projects in GitHub directory use individual-related configurations in the ~/gitconfig/.gitconfig_individual file;
  • The global ignore file is placed in the ~/gitconfig/.gitignore_global file, so that there is no need to create .gitignore for each project;
  • Define the default global user.

In this way, you only need to put the project in the right directory, and there will be no confusion. Even if it is not placed in GitHub and workspace directories, there will be a default global user to protect it, so there will be no strange user submissions.

last step

It’s not over yet. After the creation is completed, you only need to add the following line to Git’s global configuration file ~/.gitconfig :

 [include] path = ~/gitconfig/.gitconfig

This article is reproduced from:
This site is for inclusion only, and the copyright belongs to the original author.