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 github.com 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 a 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 https://github.com/XXX
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 login 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 is pushed! 🎉You can now use https whenever you are out drinking coffee and ssh whenever you are at home or in the office.