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

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

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


当前回答

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

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

其他回答

一个命令github账户切换

这个解决方案采用一个git别名的形式。一旦执行,当前项目用户将附加到另一个帐户

生成ssh密钥

ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'

[...]

ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'

将它们链接到你的GitHub / Bitbucket帐户

复制默认公钥pbcopy < ~/.ssh/id_rsa.pub 登录到你的GitHub账户 粘贴密钥在添加SSH密钥github页面 复制其他公钥pbcopy < ~/.ssh/id_rsa_pro.pub 对其他帐户重复并调整步骤2至4

步骤1。ssh密钥自动切换。

我们可以将ssh配置为根据主机使用特定的加密密钥发送。好处是同一个主机名可以有多个别名。

请看这个例子~/。ssh /配置文件:

# Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

# Professional github alias
Host github_pro
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_pro

Git远程配置

现在可以在git远程中使用这些别名,方法是将git@github.com改为git@github_pro。

您可以更改现有的项目远程(使用类似git remote set-url origin git@github_pro:foo/bar.git之类的东西),或者在克隆它们时直接调整它们。

git clone git@github.com:ArnaudRinquin/atom-zentabs.git

使用别名,它变成:

git clone git@github_pro:ArnaudRinquin/atom-zentabs.git

步骤2。修改git user.email

Git配置设置可以是全局的,也可以是每个项目的。在本例中,我们需要一个每个项目的设置。更改它很容易:

git config user.email 'arnaud.rinquin@wopata.com'

虽然这很容易,但这让我们对开发者产生了渴望。我们可以为此编写一个非常简单的git别名。

我们将把它添加到~/。gitconfig文件。

[user]
    name = Arnaud Rinquin
    email = rinquin.arnaud@gmail.com

...

[alias]
    setpromail = "config user.email 'arnaud.rinquin@wopata.com'"

然后,我们所要做的就是git setpromail来改变我们的电子邮件仅为这个项目。

步骤3。一个命令开关?!

从默认帐户切换到使用单一无参数命令的指定帐户不是很好吗?这绝对是可能的。这个命令有两个步骤:

将当前项目远程更改为所选别名 修改当前业务群组用户。邮件配置

对于第二步,我们已经有了一个命令解决方案,但第一步要困难得多。 一个命令远程主机更改

下面是另一个git别名命令的解决方案,可以添加到~/.gitconfig中:

[alias]
  changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"

这允许将所有远程服务器从一个主机更改为另一个主机(别名)。请看例子:

$ > git remote -v
origin  git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin  git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)

$ > git changeremotehost github.com github_pro

$ > git remote -v
origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)

把它们结合起来

现在我们只需要把这两个命令合并成一个,这很简单。看看我是如何整合比特桶主机切换的。

[alias]
  changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
  setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
  gopro = !sh -c \"git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail\"

来源链接-Github

源链接-教程

你也可以使用git commit——author "Your Name <your@email.com>"在你想以不同的用户提交的repo中进行提交。

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

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

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

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

如果你不想有一个默认的电子邮件地址(电子邮件地址链接到一个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"

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