Opinions about designing, programming and using software

git permission denied (publickey)

The Project

The distributed version control system git is quite popular in the open source world, and github is a service that provides a commercial git server, along with simple wikis and bug trackers.  Many open source projects are now hosted on github, allowing easy collaboration, maintenance and even forking and merging of projects.

Because we just released a small open source project, I thought that we’d try hosting a copy of the code on github.  It would give me a chance to explore the feature set and get some practice with git at the same time.

The Problem

I got our account set up and installed Git for Windows.  I then set up a local repository, added an origin (an entry identifying the remote server to use) generated a key pair and added the public key to the git account, following github’s instructions.  After adding files to the local repository and committing the changes, I tried to push the new version up to github and got an error:

$ git push origin master
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Troubleshooting

A quick search will turn up various references to this error.  I looked at github’s troubleshooting page.  The first suggestion it makes is to test ssh directly:

$ ssh git@github.com
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.

I did this and was immediately prompted for my passphrase. I entered it and was successfully connected to the server, then disconnected with the message above. Since git wasn’t prompting me for my passphrase, I decided to start there. Github helpfully provides instructions on using passphrases with git, so I read that. Let me save you a little time right now: if you just installed Git for Windows and don’t have a separate installation of msys, you probably don’t have a .bashrc. It should be in your “home” directory, which is one level above your “My Documents” directory on Windows. If you don’t find one there, just create one using the content suggested on github’s page.

The instructions suggested configuring bash to run ssh-agent on start-up. This results in having to supply the passphrase only once per bash session. I restarted git-bash and supplied my passphrase when prompted. I was then able to ssh to git. I retried the push command and got the same error. I then tried using ssh in verbose mode, as suggested in the troubleshooting guide:

ssh -v git@github.com
# Lots of output
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.

I haven’t included all of the output, but it confirmed that ssh was using the correct key. I don’t imagine that I would have gotten the success message on prior attempts otherwise, but it was worth verifying. This support discussion at github provides an example of the output that you’ll see.

After removing and readding the public key with no better results, I decided that the problem had to be with the git configuration somehow.  Checking the git documentation, I found that you can set the environment variable $GIT_TRACE to enable verbose debug output when running git, much like the -v option for ssh.

$ GIT_TRACE=true
$ export GIT_TRACE
$ git push origin master
setup: git_dir: .git
setup: worktree: c:/Documents and Settings/user/My Documents/Projects/project
setup: cwd: c:/Documents and Settings/user/My Documents/Projects/project
setup: prefix: (null)
trace: built-in: git 'push' 'origin' 'master'
trace: run_command: 'ssh' 'get@github.com' 'git-receive-pack '\''username/project.git'\'''

Look carefully at that last line. Note the “e” in “get@github.com”. If you look back at the successful ssh command, the username specified “git”, not “get”. To make sure that this wasn’t some oddity in how github worked that I had missed, I checked their support and found this thread, which made my mistake very obvious.

The Solution

When adding the reference to the origin, I had made a typo. Fixing it was easy:

$ git remote rm origin
$ git remote add origin git@github.com:username/project.git

Once I fixed the definition of origin, everything worked as expected.

9 Comments

  • Posted March 3, 2011 at 3:34 am | Permalink

    Very good post.

  • Posted June 11, 2011 at 3:21 pm | Permalink

    Excellent Post! Thanks for sharing the details!

  • crimaniak
    Posted November 23, 2011 at 5:31 pm | Permalink

    Very usefull, thanks!!!

  • crimaniak
    Posted November 23, 2011 at 5:35 pm | Permalink

    BTW, instead of ‘git remote rm’ and ‘git remote add’ it’s possible to just edit origin in .git/config file (remote “origin” section, url variable).

    • Posted November 29, 2011 at 8:39 pm | Permalink

      Would you have to edit the .git/config file for all .git directories in any subdirectories as well? Either way, it’s nice to know that there’s an alternative. Thanks.

  • Hugo
    Posted June 14, 2012 at 2:32 am | Permalink

    This was help full. Thanks a lot!

  • hello
    Posted January 6, 2013 at 11:17 am | Permalink

    on .bashrc: “If you don’t find one there, just create one using the content suggested on github’s page”

    github’s page does not seem to explicitly state how to create my own ~/.bashrc.

    any helpful links you can share?

  • Posted October 21, 2013 at 12:42 am | Permalink

    Thank you, that helped!

One Trackback

  • By git permission denied (publickey) on February 8, 2011 at 8:33 pm

    [...] along with simple wikis and bug trackers. Many open source projects are now hosted on github,… [full post] Charles Calvert Charles on Software softwareversion controlgitgithub 0 0 [...]

Post a Comment

Your email is kept private. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>