在我的~/。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并使用-c标志来覆盖全局和特定于存储库的配置。
例如,通过定义别名:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
要查看它是否有效,只需输入git config user.email:
$ git config user.email
name@example.com
你也可以在$PATH中放置一个自定义的git可执行文件,而不是一个别名。
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
这些方法相对于特定于存储库的.git/config的优点是,当自定义git程序处于活动状态时,它适用于每个git存储库。通过这种方式,您可以轻松地在用户/名称之间切换,而无需修改任何(共享的)配置。
在看完所有的答案后,这就是我的解决方案:
场景:
本地操作系统:Linux (bash)
GitHub上的两个账户(工作/个人)
不同密钥的SSH访问
我的工作帐户将是“主”帐户(因此不需要自定义配置)
配置:
Set a "fake" host, which allows me to select the proper SSH keypair.
At ~/.ssh/config:
# Personal GitHub account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes
"Tell" git it should use a custom configuration within a given directory subtree:
At ~/.gitconfig:
# My personal projects
[includeIf "gitdir/i:~/REPOS/PERSONAL/"]
path = ~/REPOS/PERSONAL/personal.gitconfig
Finally, my config at ~/REPOS/PERSONAL/personal.gitconfig:
# gitconfig for personal projects
[user]
email = personalemail@example.com
[url "git@github-personal"]
insteadOf = git@github.com
一旦上述三个到位,我可以克隆一个个人回购像这样:
user@host:~/REPOS/PERSONAL$ git clone git@github.com:jmnavarrol/python-multigit.git
Cloning in 'python-multigit'...
(...)
user@host:~/REPOS/PERSONAL$ cd python-multigit && git remote -v
origin git@github-personal:jmnavarrol/python-multigit.git (fetch)
origin git@github-personal:jmnavarrol/python-multigit.git (push)
user@host:~/REPOS/PERSONAL/python-multigit$
在~/REPOS/PERSONAL/ PERSONAL .gitconfig添加了url重写…
[url "git@github-personal"]
insteadOf = git@github.com
...是什么允许我仅通过脚本中的“标准”URL引用repo, subrepos(即python-multigit本身的情况-是的,蹩脚的自我推销尝试),而不用冒使用“错误”URL的风险,因此,错误的身份验证。
我的意见。