在我的个人计算机上,我已经在我的全球git配置中设置了我的个人电子邮件地址。
$ git config --global --get user.email
steve@personal.com然而,我也有我的公司的代码签出,因此,我需要配置git与我的公司的电子邮件地址。
$ cd corp/project
$ git config --local --get user.email
steve@corp.com然而,有时,当克隆回购时,我忘记覆盖我的电子邮件地址,所以我承诺使用我的个人电子邮件地址。
可以删除我的全局git配置,从而防止我在本地git配置中设置user.email之前在任何回购中提交。
不过,这有点像个皮塔,在理想的世界里,我可以设置一个层次化的git配置,以便在某个子目录下的repos (或确定应用哪个配置的其他方法)中使用其中最具体的设置。
如下所示:
~/
|
+--- .gitconfig # sets personal email address
|
+--- src/
|
+--- project/ # ~/.gitconfig email address applies
|
+--- corp/
|
+--- .git/config # sets corp email address
|
+--- project/ # corp/.git/config email address appliesAFAIK目前这在git中是不可能的,它需要一个介于全局和本地之间的新配置级别。
有什么办法让我达到我在这里想要的目标吗?
发布于 2017-05-01 05:28:24
它还不受支持,但是,从git 2.8 (Mar '16)开始,您可以禁用全局用户配置,如下所示:
git config --global user.useConfigOnly true这样,git就不会让您提交,除非您在本地配置中设置电子邮件。不过,您可以想出一个脚本来获取全局设置,并将其复制到本地配置以获得快速解决方案。
发布于 2017-05-01 05:40:01
结帐这个伟大的职位 on 吉特钩,特别是电子邮件的使用。他完全放弃了全局配置,并使用钩子来防止没有配置可用的克隆:
EMAIL=$(git config user.email)
if [ -z "$EMAIL" ]; then
# user.email is empty
echo "ERROR: [pre-commit hook] Aborting commit because user.email is missing. Configure user.email for this repository by running: '$ git config user.email name@example.com'. Make sure not to configure globally and use the correct email."
exit 1
else
# user.email is not empty
exit 0
fi使用它的方法是在文章中。您可以对此进行改进,以便在存储库根中查找本地配置,检查电子邮件(甚至使用grep ),如果不存在,请使用全局配置。有点像
EMAIL = $(grep user.email $GIT_DIR/config)
if [[ $? ]]; then
EMAIL = $(git config user.email)
exit 0
fi
EMAIL = $(EMAIL##*user.email* )当钩子运行时,GIT_DIR保证是存储库根。
发布于 2017-05-01 05:52:32
AFAIK git本身不支持超过每个回购和全球身份。
我使用zsh中的cd钩子归档了类似的内容
# ~/.zshrc
# call this function after cd-ing into a directory
__zsh-on-cd () {
if git ls-files &>/dev/null ; then
if [[ "$PWD" =~ 'Company' ]]; then
echo "setting git to use company identity"
git config user.name "John Doe"
git config user.email "doe@company.com"
else
echo "setting git to use personal identity"
git config user.name "johndoes"
git config user.email "me@personal.domain"
fi
fi
}
chpwd_functions=(${chpwd_functions[@]} "__zsh-on-cd")https://stackoverflow.com/questions/43714346
复制相似问题