Tutorial

Git Tips: Configuring Exclusive User Info and SSH Keys for Different Projects

2025-08-07 #Git

In daily development, we might need to maintain both personal and work projects simultaneously, which require different Git user information (username, email) and SSH keys for committing and pushing. This article introduces two core methods to help you elegantly manage multiple Git identities.

First, we need to understand Git's global configuration, which is the default setting for all repositories.

1
2
3
# 设置全局默认的用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

View information

1
2
git config user.name
git config user.email

Configuring Different Commit Authors

This section only affects the author information recorded during git commit.

Scenario A: Configuring for a Single Project (Simple and Direct)

If you only occasionally need to use different user information for a specific project, the simplest method is to execute the git config command in that project's directory. This creates or modifies the .git/config file within the project, which takes precedence over the global configuration.

1
2
3
4
5
6
# 进入你的项目目录
cd /path/to/your/project

# 为此项目单独设置用户名和邮箱
git config user.name "Work Name"
git config user.email "[email protected]"

Scenario B: Auto-switching for Specific Directories (Once and for All)

If you want all Git repositories under a certain directory (such as ~/work/) to automatically use the same set of user information, you can use the includeIf feature introduced in Git version 2.13 for smart switching.

Step 1: Create a dedicated configuration file

Create an independent configuration file for your work identity, for example, ~/.gitconfig-work.

1
2
3
4
# ~/.gitconfig-work 文件内容
[user]
name = Work Name
email = [email protected]

Step 2: Modify the main configuration file

Edit your global Git configuration file ~/.gitconfig, adding the includeIf directive. It will determine whether the current repository is under your specified directory; if so, it loads the corresponding configuration file.

1
2
3
# ~/.gitconfig 文件末尾添加
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
  • gitdir:~/work/: Indicates that this rule applies to the ~/work/ directory and all its subdirectories. The trailing / in the path is important.
  • path = ~/.gitconfig-work: Specifies the path of the configuration file to be loaded.

Once configured, all projects located under the ~/work/ directory will automatically use Work Name as the commit author.

Using Different SSH Keys (Push Authentication)

user.name and user.email only affect commit records, while authentication for pushing to remote repositories (such as GitHub, GitLab) depends on SSH keys. If you need to push code to repositories under different accounts, you need to configure multiple SSH keys.

Step 1: Edit the SSH configuration file

Open or create the ~/.ssh/config file to set up aliases for different Git service accounts:

1
2
3
4
5
6
7
8
9
10
11
12
# 默认 GitHub 个人账号
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# 工作用 GitHub 账号
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes
  • Host github.com-work: Defines an alias to be used in Git remote URLs later.
  • IdentityFile ~/.ssh/id_rsa_work: Specifies the private key file used by this alias.

Step 2: Test the SSH connection

Run the following command to test if the newly configured SSH connection is successful.

1
2
3
ssh -T [email protected]
# 如果成功,你会看到类似信息:
# Hi work-username! You've successfully authenticated...

Step 3: Modify the project's remote repository address

Go into the project that needs to use the work key and modify the URL of its remote repository (origin), replacing github.com with the alias you just configured github.com-work.

1
2
3
4
5
6
7
# 查看当前的远程地址
git remote -v
# origin [email protected]:work-org/project.git (fetch)
# origin [email protected]:work-org/project.git (push)

# 修改远程地址
git remote set-url origin [email protected]:work-org/project.git

How to Verify the Configuration

If you are unsure which configuration is ultimately active in the current project, you can use the following command to check, which will clearly display the source of each configuration item.

1
2
3
4
# 查看所有生效的配置及其来源文件
git config --list --show-origin

# 按 'q' 键退出

Summary

  • git config user.name/email: Determines who committed the code (author information).
  • ~/.ssh/config and git remote: Determines which identity you use to push to the remote repository (authentication information).

By combining includeIf and SSH config, you can easily achieve seamless isolation between personal and work environments, making Git management more efficient and professional.

Comments
Share

Comments