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

后端 未结 6 728
慢半拍i
慢半拍i 2020-12-04 04:40

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 orig         


        
相关标签:
6条回答
  • 2020-12-04 04:57

    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/
    
    0 讨论(0)
  • 2020-12-04 04:58
    • 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.

    0 讨论(0)
  • 2020-12-04 05:13

    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

    • "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
    0 讨论(0)
  • 2020-12-04 05:16

    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.

    0 讨论(0)
  • 2020-12-04 05:17

    You need to clone in ssh not in https.

    For that you need to set your ssh keys. I have prepared this little script that automates this:

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

    and run it like

    bash script.sh myemail@example.com github.com
    

    Change your remote url

    git remote set-url origin git@github.com:user/foo.git
    

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

    Check connection

    ssh -T git@github.com
    
    0 讨论(0)
  • 2020-12-04 05:18

    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
    
    0 讨论(0)
提交回复
热议问题