在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。

但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。

是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?


当前回答

Windows环境

另外,如果您在系统中安装了Git Extensions -> Settings -> Global Settings,则可以修改此选项。

gitextensions-latest-release

右键单击Windows环境中的文件夹/目录以访问这些设置。

更新:如何在2.49版本中切换/维护多个设置

其他回答

如果你不想有一个默认的电子邮件地址(电子邮件地址链接到一个github用户),你可以配置你想要被问到。如何做到这一点取决于你使用的git版本,见下文。

(预期的)缺点是您必须为每个存储库配置一次电子邮件地址(和您的姓名)。所以,你不能忘记去做。

版本< 2.7.0

[user]
    name = Your name
    email = "(none)"

在全局配置中~/。Dan Aloni在Orr Sella的博客文章中评论了gitconfig。当尝试在存储库中进行第一次提交时,git失败了,并给出了漂亮的消息:

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '(none)')

当本地设置电子邮件地址时,该名称取自全局配置(消息不是完全准确)。

2.7.0≤版本< 2.8.0

版本< 2.7.0中的行为不是预期的,并在2.7.0中修复。您仍然可以使用Orr Sella的博客文章中描述的预提交钩子。此解决方案也适用于其他版本,但其他解决方案不适用于此版本。

版本≥2.8.0

Dan Aloni添加了一个选项来实现这种行为(参见发布说明)。用它:

[user]
    useConfigOnly = true

为了让它工作,你可以不给一个名字或电子邮件地址在全局配置。然后,在第一次提交时,您会得到一条错误消息

fatal: user.useConfigOnly set but no name given

因此,该消息不是很有指导意义,但由于您显式地设置了该选项,您应该知道该做什么。与< 2.7.0版本的解决方案相反,您总是必须手动设置名称和电子邮件

$ git config user.name "Your Name"
$ git config user.email "your@email.com"

它只在当前存储库中设置您的姓名和电子邮件。

您可以将单个回购配置为使用覆盖全局配置的特定用户/电子邮件地址。从回购的根目录运行

git config user.name "Your Name Here"
git config user.email your@email.com

而缺省用户/电子邮件是在~/.gitconfig中配置的

git config --global user.name "Your Name Here"
git config --global user.email your@email.com

也许这是一个简单的黑客,但它是有用的。只需生成如下所示的2个ssh密钥。

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+.      |
|.o o+o.o=        |
|o o o.o. +       |
| =.+ .  =        |
|= *+.   S.       |
|o*.++o .         |
|=.oo.+.          |
| +. +.           |
|.o=+.            |
+----[SHA256]-----+

同样的方法为个人创造一个。因此,您有两个ssh密钥,work和company。复制工作。酒吧,工作,个人。Pub, personal to ~/。ssh /目录。

然后使用以下代码创建一个shell脚本,并将其命名为crev.sh (Company Reverse),并包含以下内容。

cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub

以同样的方式,用以下内容创建一个名为prew .sh(个人反向)的文件。

cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub

在~ /。Bashrc为这些脚本添加别名如下所示

alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc

无论何时你想使用company,只要使用crev,如果你想使用personal,请使用prev:-p。

将这些ssh密钥添加到您的GitHub帐户。请确保之前没有生成id_rsa,因为这些脚本将覆盖id_rsa。如果您已经生成了id_rsa,请将其用于其中一个帐户。将它们复制为个人密钥,跳过生成个人密钥。

或者您可以在本地的.git/config文件中添加以下信息

[user]  
    name = Your Name
    email = your.email@gmail.com

只需将此添加到~/。Bash_profile在github.com的默认键之间切换

# Git SSH keys swap
alias work_git="ssh-add -D  && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"