title: git push with ssh to server , public key error
body: <p>I have ec2 server with ubuntu 12.04, I have added one more user &quot;git&quot; to server. Generated ssh key and uploaded to both users .ssh directory. So now i can access my server as</p>
  <pre><code>ssh ubuntu@example.com
  ssh git@example.com
  </code></pre>
  <p>I have created a bare repository to <code>/opt/git/gitpg.git</code>  and added this as remote to my local repository as</p>
  <pre><code>sudo git remote add deploy ubuntu@example.com:/opt/git/gitpg.git
  </code></pre>
  <p>When i tried to push to this repository, it shows authentication error.</p>
  <pre><code>sudo git push deploy master
  Permission denied (publickey).
  fatal: The remote end hung up unexpectedly
  </code></pre>
  <p>I also tried <a href="https://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-without-using-ssh-config">this solution</a>, but it also doesn't work.</p>
  <p>Can anyone please suggest what may be the possible reason ?</p>
  <p><strong>Update</strong></p>
  <p>When i tried <code>git push deploy master</code> instead of <code>sudo git push deploy master</code> it succeed to connect but unable to push due to permission.</p>
  <pre><code>Counting objects: 5, done.
  Delta compression using up to 4 threads.
  Compressing objects: 100% (3/3), done.
  Writing objects: 100% (3/3), 356 bytes, done.
  Total 3 (delta 1), reused 0 (delta 0)
  To ubuntu@example.com:/opt/git/gitpg.git
     d6cc9c3..25ddaa4  master -&gt; master
  error: unable to create directory for .git/refs/remotes/deploy/master
  error: Cannot lock the ref 'refs/remotes/deploy/master'.
  </code></pre>
  <p><strong>Update 2</strong></p>
  <p>I changed local directory permission to current user now it working without sudo.
  But i still don't know why it is nor working with sudo.</p>