338

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

git@github.com: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?

corazza
  • 31,222
  • 37
  • 115
  • 186
nikhil
  • 8,925
  • 21
  • 62
  • 102
  • 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](https://stackoverflow.com/q/1722807/608639). – jww Nov 16 '17 at 21:02

9 Answers9

441

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 git@github.com: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 = git@github.com:nikhilbhardwaj/abc.git

A shortcut is to use the set-url command:

$ git remote set-url origin git@github.com:nikhilbhardwaj/abc.git

More information about the SSH-HTTPS switch

Community
  • 1
  • 1
David Cain
  • 16,484
  • 14
  • 65
  • 75
  • Thanks, I didn't know about them making smart https the default. – nikhil Jun 26 '12 at 09:29
  • 9
    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. – MestreLion Sep 14 '12 at 13:13
  • 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. – Kemal Dağ Oct 11 '13 at 08:39
  • 1
    Now that GitHub is deprecating password access ([see here](https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/)), it seems like this answer needs to be part of their official documentation somewhere. Is it already there? – Matt Moehr Jan 29 '21 at 20:21
  • For anyone whom this didn't work check your global git config at ~/.gitconfig. I had an insteadOf url command forcing https access. – Declan McKenna May 15 '23 at 10:55
329
  • GitHub

    git config --global url.ssh://git@github.com/.insteadOf https://github.com/
    
  • BitBucket

    git config --global url.ssh://git@bitbucket.org/.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.

corazza
  • 31,222
  • 37
  • 115
  • 186
Trevor Austin
  • 3,315
  • 1
  • 11
  • 3
  • 4
    If anyone wants to look this up in [the documentation](https://www.kernel.org/pub/software/scm/git/docs/git-config.html), search for `url..insteadOf`. –  Jun 18 '14 at 15:39
  • 3
    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) – tdc Oct 23 '15 at 20:45
  • 1
    For gitlab: git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/ – MoOx Apr 08 '16 at 13:23
  • git config --global url.ssh://git@github.com/.insteadOf https://github.com/ - warning, i tried this, and it break all my connections with git – Itai Spector Jun 09 '16 at 16:05
  • 3
    I _think_ that it should be git config --global url.ssh://git@github.com:.insteadOf https://github.com/, because github likes git@github.com:/.git. (EDIT `git config --global url.git@github.com:.insteadOf https://github.com/` works in git 2.7.4 for sure.) – Glen Keane Jun 22 '16 at 11:51
  • This is the correct answer if you are working cross platform and the other people want to use `https` because they're on windows. – Pylinux Jun 27 '17 at 11:01
  • 2
    Since a comment here mentioned homebrew problems it might be a good idea to remove `--global` and do this on a pr repo basis. – Pylinux Jun 27 '17 at 11:02
  • This doesn't work, at least for existing repositories. – Andrew Koster Sep 04 '19 at 17:10
  • 1
    @Pylinux 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. – Raman Aug 12 '20 at 17:25
  • @AndrewKoster seems to work here for pulling an existing git repository. – timwaagh Nov 25 '22 at 15:38
  • 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. – jp_ Jul 20 '23 at 06:16
120

The response provided by Trevor is correct.

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

# Enforce SSH
[url "ssh://git@github.com/"]
  insteadOf = https://github.com/
[url "ssh://git@gitlab.com/"]
  insteadOf = https://gitlab.com/
[url "ssh://git@bitbucket.org/"]
  insteadOf = https://bitbucket.org/
Community
  • 1
  • 1
MoOx
  • 8,423
  • 5
  • 40
  • 39
  • 4
    Much simpler +1 – PiersyP Mar 28 '17 at 16:12
  • *+1* for this trick. It is also recommended by the kernel folks. Also see [git pull](https://lists.kernelnewbies.org/pipermail/kernelnewbies/2017-November/018413.html) on the kernel newbies mailing list. – jww Nov 16 '17 at 20:52
  • 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. – colm.anseo May 27 '18 at 01:23
  • 2
    For Gitlab: `[url "ssh://git@gitlab.com/"]` `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. – Beni Cherniavsky-Paskin Jun 10 '18 at 08:46
  • This doesn't work, at least for existing repositories. – Andrew Koster Sep 04 '19 at 17:11
  • To be specific, modify the file via: `$> vi ~/.gitconfig` – spdrman Aug 30 '23 at 03:45
  • For GitHub Gists you may want to add `[url "git@github.com:"]` `insteadOf = https://gist.github.com//` – Alex Jasmin Aug 31 '23 at 03:21
10

You need to clone in ssh not in https.

$ ssh-keygen -t ed25519 -C "your_email@example.com"

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 your_email@example.com github.com

Change your remote url

git remote set-url origin git@github.com: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 git@github.com
Matt Bussing
  • 556
  • 6
  • 10
rofrol
  • 14,438
  • 7
  • 79
  • 77
  • This was handy for me except I had to change `Hostname $hostname *.$hostname` to `Hostname $hostname` for it to work. – silleknarf Mar 08 '22 at 20:11
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 = git@github.com:username/repo.git
bhargav joshi
  • 329
  • 3
  • 6
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.

Mike Lyons
  • 1,748
  • 2
  • 20
  • 33
2

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

[url "git@github.com:"]
    insteadOf = https://github.com/
[url "git@gist.github.com:"]
    insteadOf = https://gist.github.com/
Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
-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.

Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
-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://git@gitlab.king.com/knight/squire.git

git pull, or whatever and enjoy your genius

candyline
  • 788
  • 8
  • 17