Reputation: 3247
I have an older BitBicket login credentials on my local machine. I need to change it to my work credentials. How do i change all that? Right now I am attempting to push to my work login credentials. I get the following and I assume it is because I need to change login credentials and ssh key?
myapp (master) ✔ git push -u origin master
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Upvotes: 1
Views: 15448
Reputation: 1687
TL;DR
I experienced this when my ssh key went msissing from my ssh agent. I solved the problem by adding it to the ssh agent.
Long answer
I restarted after installing an update on my Macbook. After restaring, first I worked on (and pushed to bitbucket) a personal project. Then I wanted work on my "work" project and push to the appropriate repo. git push
failed this time.
Setting GIT_SSH_COMMAND=ssh -v
helped identify that the ssh key of my personal account was being used. Then I checked the keys added to SSH agent, only my personal account was listed.
$ ssh-add -l
3072 SHA256:xxx...xxx raiyan_kamal (RSA)
I also checked with ssh -T
$ ssh -T
logged in as raiyan_kamal
...
The SSH key for my work account is work_key and name is raiyan@work. So now I added the appropriate key to my agent:
ssh-add <path to my work_key private key file>
Then checked to make sure, both keys were listed:
$ ssh-add -l
3072 SHA256:xxx.xxx raiyan_kamal (RSA)
2048 SHA256:yyy.yyy raiyan@work (RSA)
Now git push
to the work project repo worked.
Upvotes: 0
Reputation: 13
Would it be much better if you could generate a new ssh pubkey and add them in your account instead?
Upvotes: 1
Reputation: 18869
One possible solution would be to setup your git host in ~/.ssh/config along with the private key that you use for work (Example ~/.ssh/id_rsa_work
. Therefore, add the following to ~/.ssh/config
:
Host workid
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_work
Then add the public key to your bitbucket account (i.e. copy the contents of cat ~/.ssh/id_rsa_work.pub
and use them in the SSH Keys section of your bitbucket account)
And then you can do a
git clone git@workid:<accountname>/<reponame>.git
For an existing repository, follow these steps:
Get a list of remotes:
git remote -v
Change the remote url:
git remote set-url origin git@workid:<accountname>/<reponame>.git
Then run the first command again to confirm that the remote urls have been updated.
The benefit of this approach is that you can setup as many SSH keys as you wish (for example, work and/or private) to access the bitbucket repo.
If going down the above route, remember to set the correct permissions and ownership for the ~/.ssh/config
file:
chown $USER ~/.ssh/config
chmod 644 ~/.ssh/config
To solve the problem of different emails, identities for each account, there exists an approach to use pre-commit hook detailed in https://stackoverflow.com/a/23107012/325742
Upvotes: 4