多个git帐户:不使用.ssh/config或SSH代理 [英] Multiple git accounts: no .ssh/config or SSH agent usage

查看:70
本文介绍了多个git帐户:不使用.ssh/config或SSH代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用多个Git服务器帐户(与GitHub,GitLab,BitBubket等一起使用).

I'd like to use multiple Git server accounts (with any of GitHub, GitLab, BitBubket etc.).

帐户通过其电子邮件地址name1@email1.com和将name2@email2.org和git设置为在相关地址中使用这些地址存储库

The accounts are distinguished by their email addresses, name1@email1.com and name2@email2.org and git is setup to use these addresses in the relevant repositories

以下约束适用于更改帐户工作流程":

The following constraints apply to the 'change-account workflow':

  1. 在不更改时更改/更改SSH配置文件(例如〜/.ssh/config 等)存储库或服务器已添加/删除/更改.
  2. 无需运行SSH代理,即无需 ssh-add ... .
  3. 不更改环境变量.
  4. 不更改 git clone< address> 指令.
  5. 将git使用的SSH密钥与其他SSH密钥隔离开.
  6. 在所有私有和公共存储库中使用相同的更改帐户工作流程".
  7. 更改帐户工作流程"是一(1)步/命令.
  8. 更改帐户工作流程"是每个存储库一(1)次(即并非每个存储库在仓库中投入工作的时间.
  1. Without touching/changing SSH config files (e.g. ~/.ssh/config, etc.) when repositories or servers are added/removed/changed.
  2. Without requiring SSH agent running i.e. no ssh-add ....
  3. Without changing environment variables.
  4. Without changing the git clone <address> instruction.
  5. Isolate the SSH keys used with git away from other SSH keys.
  6. Use the same 'change-account workflow' across all private and public repositories.
  7. The 'change-account workflow' is a one (1) step/command.
  8. The 'change-account workflow' is one (1) time per repository (i.e. not each time you move into work on the repository).

初始存储库设置(这也是一次活动)可能涉及以下内容:一步.只要不破坏约定,任何约定/假设都是可以接受的约束.

The initial repository setup (which is also one time activity) can involve more than one step. Any conventions/assumptions are acceptable as long as they don't break the constraints.

这些是不满足约束条件的响应.
它们通常需要更改〜/.ssh/config 和/或涉及SSH代理守护程序:

These are responses that do not satisfy the constraints.
They generally require changes to ~/.ssh/config and/or involve the SSH agent daemon:

  1. handle-multiple-git-account
  2. using-multiple-git-accounts
  3. multiple-git-accounts-and-ssh-key
  4. multiple-github-accounts-ssh-config
  5. multiple-git-users-on-same-machine
  6. multiple-github-accounts-with-git-in-windows
  7. git-multiple-accounts-and-repository-问题
  8. ssh-config-to-access-multiple-repo-not-工作
  9. 多个github-accounts-on-the-same-计算机
  10. can-i-specify-multiple-users-for-myself-in-gitconfig
  11. setting-up-ssh-config-file-for-多个代码库帐户
  12. how-can-i-push一台机器上-git-with-multipleaccounts
  13. multiple-github-accounts-what-values-for-host-in-ssh-config
  14. how-to-configure-multiple-github-accounts在计算机上
  15. ssh-config-with-multiple-keys-for-multiple-gitlab-用户帐户
  16. github-multiple-accounts-permission-to-personalusername-reponame-git-denied-to
  1. handle-multiple-git-account
  2. using-multiple-git-accounts
  3. multiple-git-accounts-and-ssh-key
  4. multiple-github-accounts-ssh-config
  5. multiple-git-users-on-same-machine
  6. multiple-github-accounts-with-git-in-windows
  7. git-multiple-accounts-and-repository-problems
  8. ssh-config-to-access-multiple-repo-not-working
  9. multiple-github-accounts-on-the-same-computer
  10. can-i-specify-multiple-users-for-myself-in-gitconfig
  11. setting-up-ssh-config-file-for-multiple-codebase-accounts
  12. how-can-i-push-git-with-multiple-accounts-on-one-machine
  13. multiple-github-accounts-what-values-for-host-in-ssh-config
  14. how-to-configure-multiple-github-accounts-on-your-computer
  15. ssh-config-with-multiple-keys-for-multiple-gitlab-user-accounts
  16. github-multiple-accounts-permission-to-personalusername-reponame-git-denied-to

推荐答案

经过测试,可以正常工作:

Tested and working:

克隆后,您只需执行一次-对于具有以下功能的任何存储库设置为支持使用多个git帐户提交的开发人员.

You do this once after cloning - for any repository that has been setup to support developers who commit using multiple git accounts.

git config --local --add include.path ../.git-config

现在,每当您从该存储库提交/推送时,都将使用该存储库的帐户.
移动时无需进行任何其他更改进入/离开该存储库.

Now, whenever you commit/push from this repository, you will use the account for this repository.
You do not need to make any further changes when you move into/out of this repository.

这是您设置的方式,以支持使用多个提交的开发人员git帐户" :

  1. 克隆存储库,然后Git提交:

pushd /src/path/to/git/repo
  tee --append .git-config > /dev/null <<'EOF'
  [core]
    sshCommand = ssh -o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i ~/.config/git/$(git config --get --local --includes user.email) -F /dev/null
  EOF
popd

假设:

  • SSH密钥文件被命名为< name@email.com> (专用)和< name@email.com> .pub (公开).
  • SSH密钥文件可以位于SSH命令可访问的任何位置.

注意:不使用多个帐户的用户不受此影响.克隆后,它们根本不包含Git包含 .git-config 的代码段.顺便说一句:SSH密钥文件的位置根据 XDG基本目录规范,但是您可以将SSH密钥放在最安全的位置,而不必更改SSH配置文件.

Note: Users who do not use multiple accounts are not impacted by this. They simply do not Git include the .git-config snippet after cloning. As an aside: the SSH key file location is per the XDG Base Directory specification, but you can place the SSH keys wherever is most secure and not have to change your SSH configuration file.

该问题表明,已将Git设置为使用用于命名SSH密钥文件的电子邮件地址来提交,因此已经完成了,但是出于完整性考虑:

The question stated that Git is setup to commit using the email address used to name the SSH key file, so this would have already be done, but for completeness:

git config --local user.email name1@email1.com

这篇关于多个git帐户:不使用.ssh/config或SSH代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆