2

I have set up git successfully and pushed my project to remote repository.

I had already setup my global user.name and user.email by executing following:

git config --global user.name 'user1'
git config --global user.email 'user1@example.com'

Now I have another repository for which I needed a different user, so I created another user specific to that repo by executing :

git config user.name 'user2'
git config user.email 'user2@example.com'

And when it try to execute git push, it is not letting me push my code and says

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

It was letting me push my changes with global user, what is different now ? I am new to git and its hard for me to get along.

Edit: I have separate ssh keys for my github account and my bitbucket account. That is, I want to ssh to my github repo using global settings and to my bit bucket repo using repo specific settings.

Sajjad
  • 853
  • 2
  • 15
  • 32

2 Answers2

1

Assuming you already have separate ssh keys for both accounts and linked to respective accounts, Following resolved my issue:

  1. Open a terminal window.
  2. Edit the ~/.ssh/config file . (If you don't have a config file, create one)
  3. Add an alias for each identity combination for example
  4. Close and save the file.
Host github.com
HostName github.com 
PreferredAuthentications publickey
IdentityFile ~/.ssh/personalid

Host bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/personalid

Load ssh keys

  1. Run ssh-agent if not already running by

    eval ssh-agent $SHELL

    $SHELL is the environment variable for your login shell.

  2. ssh-add -l

  3. Load your newly added key if it is not already loaded by ssh-add /path/to/yourKey

Done!

Source : Here

Sajjad
  • 853
  • 2
  • 15
  • 32
0

Changing the username and user email for a git repo has nothing to do with the ssh key which will be used for pushing to the upstream repo.

That ssh key is by default ~/.ssh/id_rsa.pub.
That public key (published on the BitBucket ssh user account page) is the one which determine who the user is when he/she pushes to BitBucket.

The user.name/user.email config is only used for displaying the right identity for the commits pushed to the BitBucket repo. It doesn't have to reflect the actual owner of said BitBucket repo (who is determined by the ssh public key, or for https protocol, by the BitBucket username/password user account).

If you want to use a different ssh public key (registered to a different BitBucket uset), then, as I mentioned in "How to change git ssh user for a remote push temporarily?", you would need a ~/.ssh/config file

git remote set-url origin anotheruser:reponame

With anotheruser being an entry in the ~/.ssh/config file, referencing anotheruser private ssh key:

Host anotheruser
HostName bitbucket.org
User git
IdentityFile ~/.ssh/anotheruser

Note that you are always using 'git' as the user for the ssh session with which you are pushing to BitBucket.
But since you are referencing the proper private (and public) ssh key, BitBucket will know who you are and, more importantly, if you have the right to push to that repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250