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

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

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


当前回答

有点像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,至少在我的目录中不需要更改。

其他回答

这个答案部分是受到@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设置将在发挥,将不允许您提交,直到用户名和电子邮件设置为回购级别,并将提示用户配置该信息。

本地邮箱/邮箱/ bashrc

.bashrc_local:不要跟踪这个文件,只把它放在你的工作电脑上:

export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"

.bashrc:跟踪这个文件,让它在工作电脑和家用电脑上都一样:

F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
    . "$F"
fi

我正在使用https://github.com/technicalpickles/homesick来同步我的dotfiles。

如果只有git配置可以接受环境变量:git配置中的Shell变量扩展

有点像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,至少在我的目录中不需要更改。

让我们重新定义规范-你只是想在git中对不同的组织使用多个身份-你只需要两句话:

 # generate a new private public key pair for org1  
 email=me@org1.eu;ssh-keygen -t rsa -b 4096 -C $email -f $HOME/.ssh/id_rsa.$email

 # use org1 auth in THIS shell session - Ctrl + R, type org1 in new one 
 email=me@org1.eu;export GIT_SSH_COMMAND="ssh -p 22 -i ~/.ssh/id_rsa.$email"

为什么我知道这是迄今为止最简单的解决方案:

  # me using 9 different orgs with separate git auths dynamically
  find $HOME/.ssh/id_rsa.*@* | wc -l
  18

在执行以下步骤之前,您必须有多个ssh密钥。这些可以使用here命令生成

步骤

转到c:\users\Your User\.ssh 触控配置 配置每个域的ssh密钥

使用gitbash去git repo。 配置用户名和名称每个回购"git config user.email="abc@gmail.com" 配置每个回购的名称。git config user.name="Mo Sh" 执行git命令。它会自动选择SSH密钥。