Issue
I'd like to use multiple Git server accounts (with any of GitHub, GitLab, BitBubket etc.).
The accounts are distinguished by their email addresses, [email protected] and [email protected] and git is setup to use these addresses in the relevant repositories
The following constraints apply to the 'change-account workflow':
- Without touching/changing SSH config files (e.g.
~/.ssh/config
, etc.) when repositories or servers are added/removed/changed. - Without requiring SSH agent running i.e. no
ssh-add ...
. - Without changing environment variables.
- Without changing the
git clone <address>
instruction. - Isolate the SSH keys used with git away from other SSH keys.
- Use the same 'change-account workflow' across all private and public repositories.
- The 'change-account workflow' is a one (1) step/command.
- 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:
- handle-multiple-git-account
- using-multiple-git-accounts
- multiple-git-accounts-and-ssh-key
- multiple-github-accounts-ssh-config
- multiple-git-users-on-same-machine
- multiple-github-accounts-with-git-in-windows
- git-multiple-accounts-and-repository-problems
- ssh-config-to-access-multiple-repo-not-working
- multiple-github-accounts-on-the-same-computer
- can-i-specify-multiple-users-for-myself-in-gitconfig
- setting-up-ssh-config-file-for-multiple-codebase-accounts
- how-can-i-push-git-with-multiple-accounts-on-one-machine
- multiple-github-accounts-what-values-for-host-in-ssh-config
- how-to-configure-multiple-github-accounts-on-your-computer
- ssh-config-with-multiple-keys-for-multiple-gitlab-user-accounts
- github-multiple-accounts-permission-to-personalusername-reponame-git-denied-to
Solution
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":
- 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
<[email protected]>
(private) and<[email protected]>.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 [email protected]
Answered By - suspended Answer Checked By - Marie Seifert (WPSolving Admin)