Git

How to Get Git to Default to SSH and Not HTTPS for New Repositories

To build a connection between remote and local repositories, the GitHub remote repositories URL can be used, such as HTTPS or SSH URLs. Suppose developers set the remote HTTPS URL as the default remote URL. In that case, it is required to provide the username and password every time whenever they need to interact with the remote repository.

On the other hand, SSH contains public and private keys. Developers need to store the public key in the GitHub repository. If it gets matched, the clone, push or pull operations can be performed without asking for a username and password.

This blog discusses the procedure of adding the SSH URL as the default remote URL.

How to Get Git SSH URL as Default Remote URL and Not HTTPS for New Repositories?

To set the SSH URL as a default remote URL, the below-listed steps can be helpful:

    • Move to the particular Git repository.
    • Check the existing remote URLs.
    • Open a web browser, move to the GitHub repository, and copy the SSH URL.
    • Run the “$ git remote set-url <remote-name> <ssh-url>” command.

Step 1: Navigate to Git Directory

First, run the “cd” command to move to the required repository by providing its path:

$ cd "C:\Users\nazma\Git"

 

Step 2: View Remote URLs List

Next, view the list of existing remote URLs through the “git remote” command:

$ git remote -v

 
Here, it can be seen that the default remote URL is HTTPs:


Step 3: Get SSH URL

Now, go to the particular GitHub repository, and click the “Code” button. Copy the SSH URL of the remote repository:


Step 4: Set SSH URL as Default Remote URL

Use the provided command with the current remote name and copy GitHub repository URL:

$ git remote set-url origin git@github.com:GitUser0422/demo5.git

 

Step 5: Verify Added SSH Remote URLs

Lastly, verify the SSH remote URL as a default remote URL for new repositories for fetching and pushing the local changes:

$ git remote -v

 

That’s all! We have described the method of adding the SSH URL as the default remote URL.

Conclusion

To set the SSH URL as a default remote URL, first, go to the particular Git repository and check the existing remote URLs. After that, open the web browser and move to the GitHub repository. Copy the remote repository SSH URL and switch back to the Git terminal. Run the “$ git remote set-url <remote-name> <ssh-url>” command. This blog explained the procedure of adding the SSH URL as the default remote URL.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.