I'm trying to adapt Specify an SSH key for git push for a given domain to my problem. The difference in my case is that I don't want to push to multiple domains, but always to the same domain as multiple (Git) users from one (Unix) account.
~/.ssh/config
is set up to have an entry for each user:
Host git-as-bob
HostName git.domain.tld
User git
IdentityFile /home/shared/.ssh/id_rsa-bob
IdentitiesOnly yes
Host git-as-alice
HostName git.domain.tld
User git
IdentityFile /home/shared/.ssh/id_rsa-alice
IdentitiesOnly yes
In one repository, I've made the necessary changes, and pushing works just fine (note: saying just git push
is supposed not to work, that's what the first command is for. It wouldn't much matter though, because the default pubkey only has read access to the repositories).
$ git remote set-url --push origin no_push
$ git remote add origin-bob git@git-as-bob:/repository1.git
$ git remote add origin-bob git@git-as-bob:/repository1.git
Here, the following happens:
$ git push origin-bob master
Everything up-to-date
In a second repository, I did the exact same thing, but the outcome is different:
$ git push origin-bob master
To git@git-as-bob:/repository2.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@git-as-bob:/repository2.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Note that git remote -v
returns virtually the same in both cases (i.e., the only difference is "repository1.git" vs. "repository2.git". Note also that prior to pushing, both repositories were in sync with origin. In the example shown here, there wasn't actually anything to push, but if there is, the same problem shows up.
If this is a sound approach, how could I troubleshoot it? If it is not, what would be the appropriate way of going about such a setup?