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

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

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


当前回答

从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]。

其他回答

虽然大多数问题在某种程度上回答了OP,但我必须自己完成这个过程,甚至不用谷歌,我就能找到最快最简单的解决方案。以下是简单的步骤:

从其他回购中复制现有的.gitconf 粘贴到您新添加的回购 修改.gitconfig文件中的值,如名称、电子邮件和用户名 (用户) 名字=约翰 邮箱= john@email.net 用户名= john133 将filename添加到.gitignore列表中,以确保不会将.gitconfig文件提交到工作回购中

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

源链接-教程

有点像Rob W的答案,但允许不同的ssh密钥,并适用于旧版本的git(没有例如核心)。sshCommand配置)。

我创建了~/bin/git_poweruser文件,具有可执行权限,并在PATH中:

#!/bin/bash

TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF

chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh

git -c user.name="Power User name" -c user.email="power@user.email" $@

每当我想以“超级用户”的身份提交或推送一些东西时,我使用git_poweruser而不是git。它可以在任何目录下工作,并且不需要更改.gitconfig或.ssh/config,至少在我的目录中不需要更改。

从Orr Sella的博客文章中得到一些灵感后,我写了一个预提交钩子(驻留在~/.git/templates/hooks中),它会根据本地存储库的。/.git/config中的信息设置特定的用户名和电子邮件地址:

你必须把模板目录的路径放到~/.gitconfig中:

[init]
    templatedir = ~/.git/templates

然后,每个git init或git克隆都将获得该钩子,并在下一次git提交期间应用用户数据。如果你想将钩子应用到已经存在的repo上,那么只需在repo中运行git init来重新初始化它。

这是我想出的钩子(它仍然需要一些抛光-欢迎提出建议)。 另存为

~/.git/templates/hooks/pre_commit

or

~/.git/templates/hooks/post-checkout

并确保它是可执行的:chmod +x ./post-checkout || chmod +x ./pre_commit

#!/usr/bin/env bash

# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"

# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"

local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"

local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"


# -------- FUNCTIONS
setIdentity()
{
    local current_id local_id

    current_id[0]="$(git config --get --local user.name)"
    current_id[1]="$(git config --get --local user.email)"

    local_id=("$@")

    if [[ "${current_id[0]}" == "${local_id[0]}" &&
          "${current_id[1]}" == "${local_id[1]}" ]]; then
        printf " Local identity is:\n"
        printf "»  User: %s\n»  Mail: %s\n\n" "${current_id[@]}"
    else
        printf "»  User: %s\n»  Mail: %s\n\n" "${local_id[@]}"
        git config --local user.name "${local_id[0]}"
        git config --local user.email "${local_id[1]}"
    fi

    return 0
}

# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"

if [[ "$current_remote_url" ]]; then

    for service in "${git_remotes[@]}"; do

        # Disable case sensitivity for regex matching
        shopt -s nocasematch

        if [[ "$current_remote_url" =~ $service ]]; then
            case "$service" in

                "${git_remotes[0]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[0]}"
                    setIdentity "${local_id_0[@]}"
                    exit 0
                    ;;

                "${git_remotes[1]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[1]}"
                    setIdentity "${local_id_1[@]}"
                    exit 0
                    ;;

                * )
                    printf "\n»  pre-commit hook: unknown error\n» Quitting.\n"
                    exit 1
                    ;;

            esac
        fi
    done
else
    printf "\n»» An Intermission\n»  No remote repository set. Using local fallback identity:\n"
    printf "»  User: %s\n»  Mail: %s\n\n" "${local_fallback_id[@]}"

    # Get the user's attention for a second
    sleep 1

    git config --local user.name "${local_fallback_id[0]}"
    git config --local user.email "${local_fallback_id[1]}"
fi

exit 0

编辑:

因此,我将钩子重写为Python中的钩子和命令。此外,还可以将脚本作为Git命令(Git passport)调用。此外,还可以在configfile (~/.gitpassport)中定义任意数量的id,这些id可以在提示符中选择。你可以在github.com上找到这个项目:Git -passport -一个用Python编写的Git命令和钩子,用于管理多个Git帐户/用户身份。

Git别名(和Git配置中的部分)来拯救!

添加别名(从命令行):

git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'

然后,以集合为例

git config --global user.github.name "your github username"
git config --global user.github.email your@github.email

在一个新的或克隆的repo中,你可以运行这个命令:

git identity github

这个解决方案不是自动的,而是在全局~/中重置用户和电子邮件。Gitconfig和设置用户。useConfigOnly设为true将迫使git提醒你在每次新的或克隆的repo中手动设置它们。

git config --global --unset user.name
git config --global --unset user.email
git config --global user.useConfigOnly true