By default, most remote git repositories support pushing to HTTPS URLs. But sometimes you may need to use SSH instead of using HTTPS. In this article, we will learn how to use SSH instead of HTTPS in git. You can use these steps with your remote git repository hosted on GitHub or BitBucket.
How to Use SSH Instead of HTTPS in Git
Here are several ways to use SSH instead of HTTPS in git.
1. Setup Repository’s Origin Branch to use SSH
If you want to use SSH protocol instead of HTTPS, just add a remote branch to use SSH, as shown below. Replace username and repo name with the names of your user and repository respectively.
$ git remote add origin email@example.com:<username>/<repo name>.git
2. Modify Existing Repository
You can always modify pre-existing repository by modifying its .git/config file. Add the following lines to it.
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* -url = https://github.com/<username>/<repo name>.git +url = firstname.lastname@example.org:<username>/<repo name>.git
You can also use a shortcut command given below to do the same thing.
$ git remote set-url origin email@example.com:<username>/<repo name>.git
In this short article, we have learnt how to use SSH for your Git repository, instead of using HTTPS URL to push commits.