在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。
但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。
是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?
Git别名(和Git配置中的部分)来拯救!
添加别名(从命令行):
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
然后,以集合为例
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
在一个新的或克隆的repo中,你可以运行这个命令:
git identity github
这个解决方案不是自动的,而是在全局~/中重置用户和电子邮件。Gitconfig和设置用户。useConfigOnly设为true将迫使git提醒你在每次新的或克隆的repo中手动设置它们。
git config --global --unset user.name
git config --global --unset user.email
git config --global user.useConfigOnly true
Git别名(和Git配置中的部分)来拯救!
添加别名(从命令行):
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
然后,以集合为例
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
在一个新的或克隆的repo中,你可以运行这个命令:
git identity github
这个解决方案不是自动的,而是在全局~/中重置用户和电子邮件。Gitconfig和设置用户。useConfigOnly设为true将迫使git提醒你在每次新的或克隆的repo中手动设置它们。
git config --global --unset user.name
git config --global --unset user.email
git config --global user.useConfigOnly true
让我们重新定义规范-你只是想在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
通过Git 2.13中的条件包含,现在可以在一台机器上用很少的工作同时拥有多个用户/电子邮件。
用户。Gitconfig有我的个人姓名和电子邮件。work-user。Gitconfig有我的工作名称和电子邮件。两个文件都位于~ path。
所以我的个人姓名/电子邮件默认适用。对于c:/work/ dir,应用的是我的工作名称/电子邮件。c:/work/github/ dir,应用我的个人姓名/邮箱。这在应用最后一个设置时起作用。
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
Gitdir区分大小写,Gitdir /i不区分大小写。
"gitdir/i:github/"将对路径中包含github的任何目录应用条件包含。