我如何配置我的 Git 身份

主要观点:作者有一篇已起草 3 年的帖子,介绍了通过gitincludeIfhasconfig:remote.*.url:等配置来根据不同条件配置gitSSH密钥,以更好地管理多个git身份和远程仓库。
关键信息:

  • includeIf可根据条件包含特定文件,如[includeIf "gitdir:~/code/**"] path = ~/.config/git/personal
  • hasconfig:remote.*.url:可根据当前工作目录的远程 URL 配置git,如[includeIf "hasconfig:remote.*.url:git@github.com:*/**"] path = ~/.config/git/config-gh
  • ~/.ssh/config可配置SSH密钥,如Host gitlab.com User git IdentityFile ~/.ssh/gitlab.id_ed25519,为不同Host设置不同IdentityFile
  • 可通过[url "gh-work:orgname"] insteadOf = git@github.com:orgnamegit配置中使用不同的Host来替换远程 URL。
    重要细节:
  • includeIf中配置的顺序很重要,后匹配的配置会覆盖先匹配的,如github.com:orgname/**要在github:*/**之后。
  • 对于ssh-agent的配置,可在~/.ssh/configIdentityFile行后添加IdentitiesOnly yes
    参考文献:列举了多个相关文章的链接。
阅读 5
0 条评论