[Fixed] Cannot Push To Github From The Command Line

Overview

Recently, I changed my laptop from Windows to a Mac. As many times setting up a new machine, I cloned my repos and installed the IDEs. A problem I encountered that I didn’t in my previous machine is that I cannot push to Github from the command line.

And yes, I generated my ssh key pair and added it to GitHub.

Despite all that, when I tried to push to GitHub from the command line, I was prompted to enter my credentials:

Github asked for credentials when pushing from the command line
Github asked for credentials when pushing from the command line

I first thought this could be a problem with Mac but that doesn’t make sense.

After searching for solutions on the internet, I surprisingly found it.

GitHub Desktop

On Windows, I didn’t use GitHub Desktop. However, when setting up my Mac, I decided to try it for experimental purposes and it’s really fun. However, for repos cloned by GitHub Desktop, the remote URL is in https format.

Git remote URL is in https format
Git remote URL is in https format

In order to push from the command line, you need to change the remote URL to the ssh format.

Which you can get on GitHub here:

The SSH git remote URL format
The SSH git remote URL format

Fixing the problem

The solution is to simply change the remote URL to SSH format, as mentioned above.

You can do that in the command line like this:

git remote set-url origin [email protected]:datmt/Spring-tutorial.git

Make sure you replace the part after origin with your own repo’s URL.

As expected, in my case, the problem is solved:

Pushing to Github through command line successfully
Pushing to GitHub through the command line successfully

How about GitHub Desktop?

GitHub desktop is quite helpful because it allows me to manage my repos in one place. However, as far as I know, when cloning repos, it uses the https URL. If you use GitHub desktop exclusively, you don’t need to do anything. However, if you work in the terminal a lot, you may want to change the remote URL to SSH format.

GitHub desktop continues to work after you change the URL format.

Leave a Comment