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

9

354

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?

Delivery answered 26/6, 2012 at 3:0 Comment(1)
I think @MoOx's answer is probably most consistent with what you are seeking. The insteadOf trick has been around since at least 2012. Also see How to convert git: urls to http: urls.Louettalough
C
459

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

Candidate answered 26/6, 2012 at 5:19 Comment(5)
Thanks, I didn't know about them making smart https the default.Delivery
This may be good for Windows users, but on Linux it was quite a step backwards: ssh always worked, and the new password caching for Smart HTTPS works only on Windows. Theres a note on "Where's the Mac version?" but not a single word for linux users.Sixteenth
I should add that, this method does not interfere with github's mac client at all. Change it and you can both use command line and gui version(github's client) of git without a problem.Ferule
Now that GitHub is deprecating password access (see here), it seems like this answer needs to be part of their official documentation somewhere. Is it already there?Radiative
For anyone whom this didn't work check your global git config at ~/.gitconfig. I had an insteadOf url command forcing https access.Hyacinthie
M
345
  • 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.

Matey answered 25/2, 2014 at 22:36 Comment(13)
If anyone wants to look this up in the documentation, search for url.<base>.insteadOf.Prudential
be wary this seems to break some things -- I've noticed some functionality of homebrew stopped working after I made this change (namely installing non-default versions / branches)Helminth
For gitlab: git config --global url.ssh://[email protected]/.insteadOf gitlab.comPyrolysis
git config --global url.ssh://[email protected]/.insteadOf github.com - warning, i tried this, and it break all my connections with gitBerna
I think that it should be git config --global url.ssh://[email protected]:.insteadOf github.com, because github likes [email protected]:<USERNAME>/<REPO>.git. (EDIT git config --global [email protected]:.insteadOf https://github.com/ works in git 2.7.4 for sure.)Richia
This is the correct answer if you are working cross platform and the other people want to use https because they're on windows.Smail
Since a comment here mentioned homebrew problems it might be a good idea to remove --global and do this on a pr repo basis.Smail
This doesn't work, at least for existing repositories.Placet
@Smail if you're going to do it on a per-repo basis, why not just set the remote URL explicitly? This is only useful globally.Delegate
@AndrewKoster seems to work here for pulling an existing git repository.Moleskin
this is much better than the accepted. in the accepted, it only tells you how to do it for a new repository. this chnges it everywhere.Helmut
This is life saver post by the way. Thanks after ~10 years.Aspectual
Use pushInsteadOf instead of insteadOf to only redirect pushes. This way you don't have to enter SSH key passphrase on pull.Slime
P
128

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/
Pyrolysis answered 8/4, 2016 at 13:27 Comment(7)
Much simpler +1Kristoferkristoffer
+1 for this trick. It is also recommended by the kernel folks. Also see git pull on the kernel newbies mailing list.Louettalough
much cleaner solution - and great for golang projects where "go get" defaults to https and one want to individually set urls to ssh instead e.g. for private repos etc.Cy
For Gitlab: [url "ssh://[email protected]/"] insteadOf = https://gitlab.com/ There is also pushInsteadOf if you want to affect push URL but not fetch. Can use git remote -v to inspect effective URLs git is going to use.Softener
This doesn't work, at least for existing repositories.Placet
To be specific, modify the file via: $> vi ~/.gitconfigBanuelos
For GitHub Gists you may want to add [url "[email protected]:"] insteadOf = https://gist.github.com/<MY_USERNAME>/Pelican
R
11

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]
Reciprocity answered 4/4, 2014 at 16:8 Comment(1)
This was handy for me except I had to change Hostname $hostname *.$hostname to Hostname $hostname for it to work.Nudibranch
K
5

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
Klina answered 15/10, 2019 at 15:10 Comment(0)
T
4

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/
Triley answered 9/11, 2021 at 23:28 Comment(0)
C
3

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.

Chyme answered 26/1, 2015 at 21:42 Comment(4)
Need to clone a new one with the ssh linkSooksoon
You can also change the repo link from HTTP to SSH, see the other answers.Chyme
why do they even make that the default way to clone? that's annoyingHobbs
@Hobbs I'd imagine because asking for authentication every time is a more secure default, and also most users may not have SSH set up yet for githubChyme
C
1

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

Cowgill answered 6/4, 2023 at 17:36 Comment(0)
K
-1

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.

Knavery answered 24/9, 2021 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.