Set up a https remote when ssh is not working

Last updated on December 17, 2021

Last week I was working from a café and I had to pull and push code to Github. But I got the following error

ssh: connect to host port 22: Operation timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

The basic explanation for this error is that I was on an unsecured network, so the git software was not able to connect to Github through ssh. But I had to push my code. I also didn't want to change my permanent settings on my mac to use https instead of ssh just because I was working from a coffee shop today (and then have to switch back to ssh later in the day).

The solution that I came up with is creating a remote called origin-https which uses https while my origin would still use ssh. Let's dive in on how you can set it up yourself.

Set up origin-https remote

To add a new remote, simply use the command

git remote add origin-https

origin-https is the name of the new remote followed by the URL of your repo. Notice that I have used the https URL instead of the ssh. You can get it this link from the Github repo.

Log in into Github using your personal access token

You might be asked to log in using your username and a password. Confusingly enough, the password you need to enter is not your Github password.

Instead, it's your personal access token. This token is used to perform git operations over https in the command line. To log in, you can follow this article by Github which has done a great job at explaining how to create a personal access token and log in from your console.

If you are not prompted for your username and password, your credentials may be cached on your computer.

Pull or Push from origin-https

Now that we have successfully created the new remote, we can pull or push using the commands

git pull origin-https BRANCH-NAME
git push origin-https BRANCH-NAME

Your code should be successfully pushed to Github! 🎉
You can now use https whenever you are out drinking coffee and ssh whenever you are at home or in the office.

Level up your web development skills

Get articles, guides and interviews right in your inbox. Join a community of fellow developers.

No spam. Unsubscribe at any time.