ssh and git pull from remote server

I'm trying to ssh from server 1 to server 2 and do a git pull from server 1 to server 2.

So on server 1, I run the following command:

ssh root@server2 'cd /root/.example; git pull origin master'

but that gives me this error:

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

Please make sure you have the correct access rights

It's able to ssh because when I do an ls (ssh root@server2 'cd /root/.example; ls) I see the contents of the /root/.example directory. It's just not able to do a git pull origin master because it uses the wrong key.

What can I do differently?

I'm using Centos 6.


You need to ssh to user who has access to git repository. If you're ssh-ing to root, make sure root has at least riead rights to repo (in case you're using some smart version of git, like gitolite, gitlab and so on).

BTW if you're trying to make release with this, use git fetch && git reset --hard origin/master instead of git pull. Git pull makes merge and it can ends with conflicts, git reset just move actual head pointer to different commit without merging anythig.