在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。

但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。

是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?


当前回答

在看完所有的答案后,这就是我的解决方案:

场景:

本地操作系统: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的风险,因此,错误的身份验证。

我的意见。

其他回答

在看完所有的答案后,这就是我的解决方案:

场景:

本地操作系统: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的风险,因此,错误的身份验证。

我的意见。

您可以将单个回购配置为使用覆盖全局配置的特定用户/电子邮件地址。从回购的根目录运行

git config user.name "Your Name Here"
git config user.email your@email.com

而缺省用户/电子邮件是在~/.gitconfig中配置的

git config --global user.name "Your Name Here"
git config --global user.email your@email.com

或者您可以在本地的.git/config文件中添加以下信息

[user]  
    name = Your Name
    email = your.email@gmail.com

在执行以下步骤之前,您必须有多个ssh密钥。这些可以使用here命令生成

步骤

转到c:\users\Your User\.ssh 触控配置 配置每个域的ssh密钥

使用gitbash去git repo。 配置用户名和名称每个回购"git config user.email="abc@gmail.com" 配置每个回购的名称。git config user.name="Mo Sh" 执行git命令。它会自动选择SSH密钥。

可以有多种方法来做到这一点,但我通过下面的shell函数来实现这一点。

function gitprofile() { if [[ $1 == "private" ]]; then if grep -q "tom@workmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi elif [[ $1 == "public" ]]; then if grep -q "tom@gmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi }

比如,

gitprofile "public" ->这将切换到配置tom@gmail.com和

gitprofile "private" ->这会切换到tom@workmail.com。

将此函数添加到~/。Bash_profile或~/。ZSHRC基于您当前的终端偏好。重新启动终端或像这样编译脚本

. ~ / . bash_profile

使函数有效。希望能有所帮助!