在我的~/。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,至少在我的目录中不需要更改。
可以有多种方法来做到这一点,但我通过下面的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
使函数有效。希望能有所帮助!