Git 用平常的事烦我
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
我通常会回应
git config user.email "me@example.com"
git config user.name "Me Name"
我不希望全局设置上述内容,因为我有多个主机并且希望为每个 SSH 主机使用不同的 Git 身份。是否可以将 Git 身份链接到 SSH 主机和/或身份,例如通过某种通用的配置文件
Host github.com
IdentityFile ~/.ssh/github_rsa
user.email "me@example.com"
user.name "Me Name"
Host gitlab.com
IdentityFile ~/.ssh/gitlab_rsa
user.email "another@example.com"
user.name "Another Name"
最佳答案
我没有看到通用的方法,但可能有这样的自定义解决方案:
为每个存储用户设置的 git 服务器创建一个包含主机名的属性文件:
echo 'user.email "me@example.com"' > $HOME/.mygitconfig/github.com.user
echo 'user.name "Me Name"' >> $HOME/.mygitconfig/github.com.user
并创建一个脚本来沿着这条线克隆一个存储库(未经测试):
cat %HOME/bin/clonerepo
#!/user/bin/bash
git clone git@$1/$2 .
while IFS=$'\n' read -r user_data; do
git config $(user_data)
done < $(HOME)/.mygitconfig/$1.user
你使用它:
$HOME/bin/clonerepo github.com path/to/repo.git
关于linux - 根据 SSH 主机或身份指定 Git 身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291450/