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

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

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


当前回答

这个答案部分是受到@Saucier的帖子的启发,但我正在寻找一种自动设置user.name和user的方法。基于远程的,每回购一次的电子邮件,这比他开发的git-passport包要轻一些。也h/t @John为useConfigOnly设置。以下是我的解决方案:

.gitconfig变化:

[github]
    name = <github username>
    email = <github email>
[gitlab]
    name = <gitlab username>
    email = <gitlab email>
[init]
    templatedir = ~/.git-templates
[user]
    useConfigOnly = true

Post-checkout钩子应该保存到以下路径:~/.git-templates/hooks/ Post-checkout:

#!/usr/bin/env bash

# make regex matching below case insensitive
shopt -s nocasematch

# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
    'github'
    'gitlab'
)

set_local_user_config() {
    local service="${1}"
    local config="${2}"
    local service_config="$( git config --get ${service}.${config} )"
    local local_config="$( git config --get --local user.${config} )"

    if [[ "${local_config}" != "${service_config}" ]]; then
        git config --local "user.${config}" "${service_config}"
        echo "repo 'user.${config}' has been set to '${service_config}'"
    fi
}

# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
    if [[ "${remote_url}" =~ "${s}" ]]; then
        set_local_user_config "${s}" 'name'
        set_local_user_config "${s}" 'email'
        break
    fi
done

我对github和gitlab使用不同的凭据,但上面代码中的那些引用可以用你使用的任何服务替换或增强。为了让签出后钩子自动在本地为签出后的回购设置用户名和电子邮件,确保服务名称出现在远程url中,将其添加到签出后脚本中的services数组中,并在.gitconfig中为它创建一个包含该服务的用户名和电子邮件的部分。

如果远程url中没有出现任何服务名称,或者repo没有远程用户名和电子邮件将不会在本地设置。在这些情况下,用户。useConfigOnly设置将在发挥,将不允许您提交,直到用户名和电子邮件设置为回购级别,并将提示用户配置该信息。

其他回答

Windows环境

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

gitextensions-latest-release

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

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

一个命令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

源链接-教程

可以有多种方法来做到这一点,但我通过下面的shell函数来实现这一点。

function gitprofile() { if [[ $1 == "private" ]]; then if grep -q "tom@workmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi elif [[ $1 == "public" ]]; then if grep -q "tom@gmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi }

比如,

gitprofile "public" ->这将切换到配置tom@gmail.com和

gitprofile "private" ->这会切换到tom@workmail.com。

将此函数添加到~/。Bash_profile或~/。ZSHRC基于您当前的终端偏好。重新启动终端或像这样编译脚本

. ~ / . bash_profile

使函数有效。希望能有所帮助!

从git 2.13开始,可以使用新引入的条件包含来解决这个问题。

一个例子:

全局配置~/.gitconfig

[user]
    name = John Doe
    email = john@doe.tld

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

工作特定的配置~/ Work /.gitconfig

[user]
    email = john.doe@company.tld

记住[includeIf…]默认的[user]后面应该跟着[user]。

让git使用多个名称/电子邮件的另一个选项是通过别名git并使用-c标志来覆盖全局和特定于存储库的配置。

例如,通过定义别名:

alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'

要查看它是否有效,只需输入git config user.email:

$ git config user.email
name@example.com

你也可以在$PATH中放置一个自定义的git可执行文件,而不是一个别名。

#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"

这些方法相对于特定于存储库的.git/config的优点是,当自定义git程序处于活动状态时,它适用于每个git存储库。通过这种方式,您可以轻松地在用户/名称之间切换,而无需修改任何(共享的)配置。