nikhil
nikhil

Reputation: 9385

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 https://github.com/nikhilbhardwaj/abc.git
git push -u origin master

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

I can manually change that to

[email protected]:nikhilbhardwaj/abc.git

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

Upvotes: 369

Views: 452512

Answers (10)

guiohm
guiohm

Reputation: 101

To automatically rewrite pushes only:

git config --global url.ssh://[email protected]/.pushInsteadOf https://github.com/

or add to your ~/.gitconfig:

[url "ssh://[email protected]/"]
    pushInsteadOf = https://github.com/

Upvotes: 0

rofrol
rofrol

Reputation: 15226

You need to clone in ssh not in https.

$ ssh-keygen -t ed25519 -C "[email protected]"

Add content of ~/.ssh/id_rsa.pub to your ssh keys on github.com.

If you need to have separate keys for different hosts, you can use this script:

#!/usr/bin/env bash

if [ $# -lt 2 ]; then
  echo "Provide email and hostname"
  exit 1
fi

email="$1"
hostname="$2"
keypath="$HOME/.ssh/${hostname}_rsa"
ssh-keygen -t ed25519 -C $email -f $keypath

if [ ! $? -eq 0 ]; then
  echo "Error when running ssh-keygen"
  exit 1
fi

exit 0
cat >> $HOME/.ssh/config <<EOF
Host $hostname
        User git
        IdentitiesOnly yes
        IdentityFile $keypath
EOF

and run it like

bash generate_ssh.sh [email protected] github.com

Change your remote url

git remote set-url origin [email protected]:user/foo.git

(or just edit .git/config)

Add content of ~/.ssh/github.com_rsa.pub to your ssh keys on github.com

Check connection

ssh -T [email protected]

Upvotes: 12

candyline
candyline

Reputation: 896

If you are using Gitlab

git remote -v

you might see something like

https://gitlab.king.com/knight/squire.git 

just replace king, knight , squire with your own thing. knight/squire is just the way our project has different directories then you can go

git remote set-url origin ssh://[email protected]/knight/squire.git

git pull, or whatever and enjoy your genius

Upvotes: 3

Cameron Tacklind
Cameron Tacklind

Reputation: 7184

While the other answers here directly answer the titular question (in a way that I didn't know was possible! TIL something new about git!) about automagically turning https based remotes into git+ssh ones, the "normal" way to do this "right" from the start is to not give git the https url.

GitHub (along with other popular git hosting services) always has a little button that lets you get the URL that git should clone. You just need to click the small "SSH" button:

Example getting the SSH url of an existing project

Alternatively for a new project

Example getting the SSH url of an new project

Once you select the "SSH" option, GitHub (and others) will remember (as long as you're logged in) and make it the default in the future.

Upvotes: -2

Devin Rhode
Devin Rhode

Reputation: 25267

FYI - I'm using this due to github no longer allowing ssh:

[url "[email protected]:"]
    insteadOf = https://github.com/
[url "[email protected]:"]
    insteadOf = https://gist.github.com/

Upvotes: 4

bhargav joshi
bhargav joshi

Reputation: 482

SSH File

~/.ssh/config file
Host *
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null
    LogLevel QUIET
    ConnectTimeout=10
Host github.com
        User git
        AddKeystoAgent yes
        UseKeychain yes
        Identityfile ~/github_rsa

Edit reponame/.git/config

[remote "origin"]
        url = [email protected]:username/repo.git

Upvotes: 6

Mike Lyons
Mike Lyons

Reputation: 1792

You may have accidentally cloned the repository in https instead of ssh. I've made this mistake numerous times on github. Make sure that you copy the ssh link in the first place when cloning, instead of the https link.

Upvotes: 3

MoOx
MoOx

Reputation: 8981

The response provided by Trevor is correct.

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

# Enforce SSH
[url "ssh://[email protected]/"]
  insteadOf = https://github.com/
[url "ssh://[email protected]/"]
  insteadOf = https://gitlab.com/
[url "ssh://[email protected]/"]
  insteadOf = https://bitbucket.org/

Upvotes: 137

Trevor Austin
Trevor Austin

Reputation: 3705

  • GitHub

    git config --global url.ssh://[email protected]/.insteadOf https://github.com/
    
  • BitBucket

    git config --global url.ssh://[email protected]/.insteadOf https://bitbucket.org/
    

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.

Upvotes: 368

David Cain
David Cain

Reputation: 17333

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 [email protected]:nikhilbhardwaj/abc.git

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 = https://github.com/nikhilbhardwaj/abc.git
    +url = [email protected]:nikhilbhardwaj/abc.git

A shortcut is to use the set-url command:

$ git remote set-url origin [email protected]:nikhilbhardwaj/abc.git

More information about the SSH-HTTPS switch

Upvotes: 472

Related Questions