Unified Git Management for Multiple Git Repositories
Note: This article was translated for me by AI. I wrote the original in Chinese. I never use AI to write my articles, because that would cost me my own expression; my freedom to express myself is always the most valuable part of my work. So if you can read Chinese, I recommend reading the Chinese version, where you will get the most original and unfiltered version. That said, technological progress exists to give us more convenience, so I will continue using AI to translate my writing into multiple languages, allowing valuable content to reach more people.
I usually deal with many projects, during this time I will encounter Git-related configuration management issues. In the past, I manually managed them, and sometimes I missed them. Now I have too many projects to handle, including the open-source projects. It is very troublesome to manage them manually. I looked at Git and found that it supports configuration based on the path.
Git Configuration Source
In general, Git reads configuration from these sources (from low to high priority):
- System-level (system) configuration:
/etc/gitconfig - User-level (global) configuration:
~/.gitconfig - Repository-level (local) configuration:
.git/configin the project
Configuration Commands
In general, we will use 2 and 3 more, that is, user-level global configuration and single repository-level configuration, which correspond to the following commands
# Global configuration
git config --global --list
# Configure username
git config --global user.name 'fake-name'
# Configure email
git config --global user.email 'fake@email.com'
# Configure GPG key ID
git config --global user.signingkey ABC123PGPKEYID
# Repository-level configuration
git config --list
# Configure username
git config user.name 'fake-name'
# Configure email
git config user.email 'fake@email.com'
# Configure GPG key ID
git config user.signingkey ABC123PGPKEYID
Unified Configuration
The above commands can be used to temporarily deal with temporary or separate settings for some repositories, but for the global level, we do not want to repeat the configuration every time we pull or start a new repository, which is very annoying, and the key point is that we often forget. Therefore, a one-time method is to configure according to the hierarchical configuration, and make the configuration in the global level, my personal habit is to divide different organizational projects according to the directory, and do not interfere with each other
For example, we have two different directories that contain various projects:
-
/path/to/github/contains some projects in the open-source community -
/path/to/fake-org/contains projects in an organization
At this time, we can configure it in ~/.gitconfig like this
[includeIf "gitdir:/path/to/github/"]
path = /path/to/.gitconfig.github
[includeIf "gitdir:/path/to/fake-org/"]
path = /path/to/.gitconfig.fake-org
In this way, we can use two different git configurations for projects in these two directories
For example, the configuration of .gitconfig.github is
[user]
name = "leo"
email = "my@email.com"
signingkey = "ABC123"
And the configuration of .gitconfig.fake-org is
[user]
name = "fake-name"
email = "fake@email.com"
signingkey = "ABC456"
In this way, we can solve the problem of different Git configurations for multiple projects once and for all
References
Enjoy Reading This Article?
Here are some more articles you might like to read next: