1

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
suspended
  • 17
  • 4

2 Answers2

2

This may not satisfy because it requires HTTPS (I'm using GitHub) If you're OK with https - I just found this simple approach which seems to work:

https://dev.to/configcat/lazy-man-s-guide-multiple-github-https-accounts-on-windows-2mad

  1. Use https paths for all checkouts (the difficult part!)
  2. Delete your GitHub credentials from Windows Credential Manager
  3. Run git config --global credential.github.com.useHttpPath true
  4. Run fetch (etc) in each repo then sign in using browser.
Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
-1

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
suspended
  • 17
  • 4