管理多个 git 提交电子邮件地址

Managing multiple git commit email addresses

我使用我的个人笔记本电脑工作(他们对此没有意见),但我注意到我所有的工作提交都使用我的个人电子邮件地址。我不想在 git 的计算机上全局默认使用我的工作电子邮件地址,有没有一种方法可以在我提交时让我的个人电子邮件地址用于某些存储库,而我的工作电子邮件用于其他存储库?

我不为重写现有提交的历史而烦恼,只是继续前进。

是的,您可以利用 --local 设置来精确管理每个存储库应使用的电子邮件地址。

# to set your work email only for the current repo
git config --local user.email '<your work email>'

# to set an email globally, as a fallthrough if a repo doesn't have a local setting for user.email
git config --global user.email '<your personal email>'

@RomainValeri 提供的解决方案之外的另一种解决方案是使用“条件包含”git 功能(我觉得更方便)

您可以将所有个人存储库放在特定文件夹中(此处 ~/personal/)并设置配置,以便在您使用 git 时,它将对其中的所有存储库使用特定配置这个文件夹。

这样,您在克隆或创建新存储库时就不会忘记设置个人设置。

你可以这样做:

~/.gitconfig

[user]
    name = John Doe
    email = john.doe@work-account.com

[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig

在包含您个人信息的新配置文件中~/personal/.gitconfig:

[user]
    email = john.doe@personal-account.com

编辑:再读一遍你的问题,因为那是你的个人电脑,也许你想做相反的事情,所以你只需要切换两个电子邮件...