如何使git默认使用ssh而不是https来创建新的代码仓库?

344

现在,当我在 GitHub 上创建新的存储库时,在设置页面上会出现以下内容:

git remote add origin https://github.com/nikhilbhardwaj/abc.git
git push -u origin master

每次我需要推送(commit)代码时,都需要输入我的GitHub用户名和密码。

我可以手动更改为

git@github.com:nikhilbhardwaj/abc.git

.git/config文件中,我觉得这非常烦人 - 有没有办法配置Git默认使用SSH?


我认为@MoOx的答案可能是最符合您要求的。 insteadOf 技巧至少自2012年以来一直存在。另请参阅如何将 git: url 转换为 http: url - jww
9个回答

448

将仓库的源分支设置为SSH

GitHub仓库设置页面只是一组建议使用的命令(而且现在GitHub建议使用HTTPS协议)。除非你拥有GitHub网站的管理员访问权限,否则我不知道有什么方法可以更改他们建议的命令。

如果您想使用SSH协议,只需像下面这样添加一个远程分支(即在GitHub的建议命令的位置使用此命令)。要修改现有分支,请参见下一节。

$ git remote add origin git@github.com:nikhilbhardwaj/abc.git

修改现有仓库

如你所知,要将一个已有的仓库从使用 HTTPS 切换为 SSH,可以在 .git/config 文件中更改远程 url。

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    -url = https://github.com/nikhilbhardwaj/abc.git
    +url = git@github.com:nikhilbhardwaj/abc.git

一个快捷方式是使用 set-url 命令:

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

关于SSH-HTTPS切换的更多信息


谢谢,我不知道他们将智能 https 设为默认。 - nikhil
9
这可能对Windows用户有好处,但在Linux上却是一步倒退:ssh始终可用,而Smart HTTPS的新密码缓存仅适用于Windows。虽然有关“Mac版在哪里?”的注释,但对于Linux用户没有一句话说明。 - MestreLion
我应该补充一下,这种方法不会干扰 Github 的 Mac 客户端。你可以更改它,这样你就可以同时使用命令行和 Git 的 GUI 版本(Github 的客户端)而不会出现任何问题。 - Kemal Dağ
1
现在GitHub正在废弃密码访问(请参见此处),似乎这个答案需要成为他们的官方文档的一部分。它已经在那里了吗? - Matt Moehr
对于任何无法正常工作的人,请检查您的全局git配置文件~/.gitconfig。我有一个insteadOf url命令强制使用https访问。 - Declan McKenna

337
  • 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/
    

    这会告诉 Git 在连接 GitHub/BitBucket 时始终使用 SSH 而不是 HTTPS,因此您将默认通过证书进行身份验证,而不是被要求输入密码。


    4
    如果有人想在文档中查找此内容,请搜索“url.<base>.insteadOf”。 - user456814
    3
    请注意,这似乎会破坏一些东西——我注意到在进行此更改后,一些 Homebrew 的功能停止工作了(特别是安装非默认版本/分支)。 - tdc
    1
    对于 GitLab:git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/ - MoOx
    5
    我认为应该是git config --global url.ssh://git@github.com:.insteadOf https://github.com/,因为github喜欢git@github.com:<USERNAME>/<REPO>.git。(编辑git config --global url.git@github.com:.insteadOf https://github.com/在git 2.7.4中肯定有效。) - Glen Keane
    3
    由于这里的评论提到了Homebrew问题,因此最好删除“--global”,并在pr repo基础上执行此操作。 - Pylinux
    显示剩余7条评论

    123

    Trevor提供的回答是正确的

    但是这里是您可以直接添加到.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/
    

    4
    好的,我会尽力做到。请提供需要翻译的内容。 - PiersyP
    这个技巧值得一试,内核开发人员也推荐使用。另请参见内核新手邮件列表上的 git pull - jww
    更干净的解决方案 - 对于 golang 项目非常有用,其中 "go get" 默认为 https,而一个人想要单独将 url 设置为 ssh,例如私有仓库等。 - colm.anseo
    2
    对于Gitlab:[url "ssh://git@gitlab.com/"] insteadOf = https://gitlab.com/ 如果您想影响推送URL而不是获取,则还有pushInsteadOf。可以使用git remote -v来检查git将要使用的有效URL。 - Beni Cherniavsky-Paskin
    这是不起作用的,至少对于现有的存储库来说是这样。 - Andrew Koster
    显示剩余2条评论

    11
    你需要使用SSH而不是HTTPS进行克隆。
    $ ssh-keygen -t ed25519 -C "your_email@example.com"
    

    ~/.ssh/id_rsa.pub的内容添加到您在github.com上的ssh键中。
    如果您需要为不同的主机使用单独的密钥,则可以使用此脚本:
    #!/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
    

    并像这样运行它

    bash generate_ssh.sh your_email@example.com github.com
    

    更改您的远程 URL

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

    (或者只需编辑.git/config

    ~/.ssh/github.com_rsa.pub的内容添加到您在github.com上的ssh密钥中

    检查连接

    ssh -T git@github.com
    

    这对我很方便,但是我必须将 Hostname $hostname *.$hostname 更改为 Hostname $hostname 才能使其正常工作。 - silleknarf

    5

    SSH文件

    ~/.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
    

    编辑 reponame/.git/config 文件

    [remote "origin"]
            url = git@github.com:username/repo.git
    

    3

    您可能不小心在https而不是ssh中克隆了存储库。 我在github上犯过这个错误很多次。 确保在克隆时首先复制ssh链接,而不是https链接。


    需要使用SSH链接克隆一个新的。 - codenamezero
    1
    您还可以将仓库链接从HTTP更改为SSH,请参见其他答案。 - Mike Lyons
    为什么他们要将那设置为默认的克隆方式?那很烦人。 - greenie-beans
    为什么他们把那设为默认的克隆方式呢?真是让人烦恼。 - greenie-beans
    @ greenie-beans 我想这是因为每次请求身份验证是更安全的默认设置,而且大多数用户可能还没有为github设置SSH。 - Mike Lyons

    3

    提醒一下 - 我使用这个是因为github不再允许使用ssh:

    [url "git@github.com:"]
        insteadOf = https://github.com/
    [url "git@gist.github.com:"]
        insteadOf = https://gist.github.com/
    

    -1

    虽然其他答案直接回答了标题问题(以一种我不知道的方式!TIL关于 git 的新内容!),即自动将基于 https 的远程转换为 git + ssh ,但“正常”方法是在开始时不提供 git https URL。

    GitHub(以及其他流行的git托管服务)始终有一个小按钮,让您获取 git 应克隆的URL。 您只需要单击小型的“SSH”按钮:

    Example getting the SSH url of an existing project

    或者对于一个新项目

    Example getting the SSH url of an new project

    一旦您选择“SSH”选项,GitHub(以及其他网站)将会记住您的选择(只要您已登录),并在未来将其设为默认选项。


    -1
    如果你正在使用Gitlab
    git remote -v
    

    你可能会看到类似这样的东西

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

    只需将king、knight和squire替换为你自己的东西。knight/squire只是我们项目中不同目录的方式,然后你就可以继续前进了。
    git remote set-url origin ssh://git@gitlab.king.com/knight/squire.git
    

    执行 git pull,或者其他操作,享受你的天赋吧。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接