linux - 根据 SSH 主机或身份指定 Git 身份

标签 linux bash git ssh configuration

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/

相关文章:

c++ - 您能否仅使用一项服务将程序与 D-Bus 消息分开?

linux - 如何将 'tee' 的参数传递给 Bash 脚本?

linux - bash:如果前一列高于 'xxx',则获取下一行

node.js - 在 Git Web hook 之后重新启动 Plesk 17.5 上的 Node 应用程序

Git:将更改应用于两个分支

python - 无法将谷歌云打印作为守护进程运行

sql - 删除损坏的 postgreSQL 表

linux - 如何以不同用户身份运行 bash 命令?

linux - Docker $(pwd) 和 bash 别名

git - 如何始终在 Git 中显示完整的历史记录?