How do I get git to default to ssh and not https for new repositories

These days when I create a new repository on GitHub on the setup page I get:

git remote add origin
git push -u origin master

And whenever I have to push a commit I need to enter my GitHub username and password.

  • Trying to download an exe file from a website and run it
  • How to get list of all branches created off a GIT branch (get all sub branches of a branch)
  • Add encrypted variables to travis.yml and push to open source project
  • Safe way to 'reshape' a branch's history
  • Anything similar to git-svn for Perforce?
  • Get the short git version hash
  • I can manually change that to

    in the .git/config. I find this quite irritating – is there some way I can configure git to use SSH by default?

  • How to rebase only the commits after the latest merge?
  • Docker: How to access to a private github repo using ssh?
  • What is the difference between develop vs. feature branch type?
  • Is it possible to create branches of git subtrees while maintaining parent repo structure?
  • Where to store release notes in Java projects that use a Gradle-Git-Artifactory stack
  • Git-tfs reset to remote
  • 5 Solutions collect form web for “How do I get git to default to ssh and not https for new repositories”

    Set up a repository’s origin branch to be SSH

    The GitHub repository setup page is just a suggested list of commands (and GitHub now suggests using the HTTPS protocol). Unless you have administrative access to GitHub’s site, I don’t know of any way to change their suggested commands.

    If you’d rather use the SSH protocol, simply add a remote branch like so (i.e. use this command in place of GitHub’s suggested command). To modify an existing branch, see the next section.

    $ git remote add origin

    Modify a pre-existing repository

    As you already know, to switch a pre-existing repository to use SSH instead of HTTPS, you can change the remote url within your .git/config file.

    [remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        -url =
        +url =

    A shortcut is to use the set-url command:

    $ git remote set-url origin

    More information about the SSH-HTTPS switch

    • “Why is Git always asking for my password?” – GitHub help page.
    • GitHub’s switch to Smart HTTP – relevant StackOverflow question
    • Credential Caching for Wrist-Friendly Git Usage – GitHub blog post about HTTPS, and how to avoid re-entering your password
    • GitHub

      git config --global url.ssh://
    • BitBucket

      git config --global url.ssh://

    That tells git to always use SSH instead of HTTPS when connecting to GitHub/BitBucket, so you’ll authenticate by certificate by default, instead of being prompted for a password.

    The response provided by Trevor is correct.

    But here is what you can directly add in your .gitconfig:

    # Enforce SSH
    [url "ssh://"]
      insteadOf =
    [url "ssh://"]
      insteadOf =
    [url "ssh://"]
      insteadOf =

    If you want many keys for different hosts, do this:

    Create a script

    #!/usr/bin/env bash
    ssh-keygen -t rsa -C $email -f $keypath
    if [ $? -eq 0 ]; then
    cat >> ~/.ssh/config <<EOF
    Host $hostalias
            Hostname $hostname
            User git
        IdentitiesOnly yes
            IdentityFile $keypath

    and run it like


    Change your remote url

    git remote set-url origin

    Add content of ~/.ssh/ to your ssh keys on

    Check connection

    ssh -T

    Make sure that you copy the ssh link in the first place when cloning, instead of the https link. That’s the PEBCAK way to ensure this default 😉

    Git Baby is a git and github fan, let's start git clone.