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.
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
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 firstname.lastname@example.org 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 email@example.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' 'firstname.lastname@example.org' 'git-receive-pack '\''username/project.git'\'''
Look carefully at that last line. Note the “e” in “email@example.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.
When adding the reference to the origin, I had made a typo. Fixing it was easy:
$ git remote rm origin $ git remote add origin firstname.lastname@example.org:username/project.git
Once I fixed the definition of origin, everything worked as expected.