Home > front end >  Using multiple credentials for same git remote address
Using multiple credentials for same git remote address

Time:04-04

I have two git repository to push

and one is in .ssh/config here

Host git-codecommit.ap-northeast-1.amazonaws.com
User AAAAAAAAAAAAA
IdentityFile ~/.ssh/id_rsa

and another is also in .ssh/config this

#Host git-codecommit.ap-northeast-1.amazonaws.com
#User BBBBBBBBBBBBB                                                                                                                            
#IdentityFile ~/.ssh/id_rsa

Both has the same address and different username

git remote -v shows like this

origin  ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/my-admin

When I did git push It will be uploaded the not-comment-out user.

So I want to switch the user in command line such as

git push -i ~/.ssh/id_rsa [email protected]/v1/repos/my-admin

However it's doesn't work ( git push doesnt have -i option)

Is there any good idea to do this??

CodePudding user response:

git push doesn't have -i option

It does not need one in your case, since the same private key, with the default name id_rsa is shared between the two accounts.

I would prefer using two separate keys, and Host entries much shorter, to emphasize you are using ~/.ssh/config entries instead of a regular SSH URL:

Host gcaws1
  Hostname git-codecommit.ap-northeast-1.amazonaws.com
  User AAAAAAAAAAAAA
  IdentityFile ~/.ssh/id_rsa

Host gcaws2
  Hostname git-codecommit.ap-northeast-1.amazonaws.com
  User BBBBBBBBBBBBB
  IdentityFile ~/.ssh/id_rsa2

And use URLs like:

origin  ssh://gcaws1/v1/repos/my-admin
# or
git push gcaws2:v1/repos/my-admin # use BBBB's key
  • Related