我有一台工作电脑,全局配置了我的工作电子邮件和姓名以进行提交。这很好。然而,我想制定一些规则,比如说:“如果仓库源是github,则使用用户X和电子邮件Y”
我知道可以针对每个仓库创建一个配置项,但我希望它更加自动化:如果克隆是来自github,则应使用github用户详细信息。如果从工作处克隆,则应使用工作详细信息。
有没有办法基于远程域来全局配置这个?还是还有别的方法?
编辑/更新:
我已接受下面的答案,但稍微修改了一下脚本:
#!/usr/bin/env bash
# "Real" git is the second one returned by 'which'
REAL_GIT=$(which -a git | sed -n 2p)
# Does the remote "origin" point to GitHub?
if ("$REAL_GIT" remote -v 2>/dev/null | grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then
# Yes. Set username and email that you use on GitHub.
export GIT_AUTHOR_NAME=$("$REAL_GIT" config --global user.ghname)
export GIT_AUTHOR_EMAIL=$("$REAL_GIT" config --global user.ghemail)
fi
"$REAL_GIT" "$@"
主要的改动是需要两个git config
值。
git config --global user.ghname "Your Name"
git config --global user.ghemail "you@yourmail.com"
这样可以避免在脚本中硬编码数值,使其更具可移植性。也许呢?