Stop User/Password Prompt when Pushing to GitHub

I began using GitHub for Mac to clone my repositories down to my Mac. Then, every time I committed and pushed something up to GitHub, I got prompted for my username and password, like this:

$ git push

After a few commits, this got annoying very quickly. I was sure that I had setup my SSH keys correctly, and  retested them to make sure. After some searching, I found the source of the problem: GitHub for Mac clones your repo using HTTPS, which subsequently causes the authentication prompt when performing a push.

Navigate to your cloned directory, and examine the .git/config file. You will see a section entitled [remote “origin”] that looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url =

Note the “url” directive pointing to your GitHub repo with the https protocol. This is what we have to change to stop the repeated authentications. From a command prompt inside your local repo directory, issue this command to change the protocol, substituting your login name and repo name where appropriate:

git config remote.origin.url [email protected]:your_username/your_project.git

In the instance of my repo, running the command looks like this:
git config remote.origin.url [email protected]:cosjef/first_app.git

After running the command, reopen your .git/config file, and you now will see that the protocol has changed:

[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:cosjef/first_app.git

If you now try a push operation, you should not be challenged for a username and password.

Creating a GitHub repository from the command line.

If you’re like me, you hate swapping out of terminal to log into GitHub when creating a new repository.  It an always jarring step that gets you out of coding flow. You can accomplish the very same thing at the command line, and never open a web browser. All you need is the cURL library (found in almost every popular flavor of Linux and OSX today), and your personal GitHub API token.

This technique calls on GitHub’s API to work its magic. You will therefore need to login to GitHub, browse to the “Your Account – Account Admin” section and find your unique API token listed there. Copy the token down for reference purposes.

When you are ready to create a new GitHub repository for your code (ie: just before “git remote add origin” step), issue the following command using the cURL URL transfer tool:

$ curl -F 'login=<your_login_name>' -F 'token=<your_API_token>' -F 'name=<your_repo_name>' -F 'description=<your_repo_summary>'

GitHub will reply to your API call with a flurry of JSON, while it creates your new repo in the background:

Your repo will now be ready to push code into. Login into GitHub to validate that it is indeed there. That’s it!

UPDATE 1/10/2013: Github terminated its API on June 1, 2012, which negates the ability to create the repo. More detail can be found here.