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

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

问题描述

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

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. 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.

Unrelated questions:

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-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:

One-step "change-account workflow":

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.

One-step Setup.

This is how you "setup to support developers who commit using multiple git accounts":

  1. Clone the repository and Git commit this:

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

Assumptions:

  • SSH key files are named <name@email.com> (private) and <name@email.com>.pub (public).
  • SSH key files can be located anywhere accessible by the SSH command.

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.

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天全站免登陆