gpt4 book ai didi

git - 是否可以在 .gitconfig 中为每个通配符域配置 user.name 和 user.email?

转载 作者:IT王子 更新时间:2023-10-29 01:02:41 25 4
gpt4 key购买 nike

我有一台工作电脑,它在全局配置为在提交时使用我的工作电子邮件和姓名。这很好。但是,我想制定某种规则,“如果 repo 来源是 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"

这避免了对脚本中的值进行硬编码,使其更具可移植性。也许吧?

最佳答案

Git 2.13 adds支持条件配置包括。如果您将 checkout 组织到每个工作域的目录中,那么您可以根据 checkout 位置添加自定义设置。在你的全局 git 配置中:

[includeIf "gitdir:code/work/"]
path = /Users/self/code/work/.gitconfig

然后在 ~/code/work/.gitconfig 中:

[user]
email = self@work.com

当然,您可以为任意多的工作领域执行此操作。

关于git - 是否可以在 .gitconfig 中为每个通配符域配置 user.name 和 user.email?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com