如何让git默认为ssh而不是https用于新的存储库

nik*_*hil 189 git ssh github

这些天,当我在设置页面上的GitHub上创建一个新的存储库时,我得到:

git remote add origin https://github.com/nikhilbhardwaj/abc.git
git push -u origin master
Run Code Online (Sandbox Code Playgroud)

每当我必须提交提交时,我需要输入我的GitHub用户名和密码.

我可以手动将其更改为

git@github.com:nikhilbhardwaj/abc.git
Run Code Online (Sandbox Code Playgroud)

.git/config.我觉得这很烦人 - 有什么办法可以配置git默认使用SSH吗?

Dav*_*ain 280

将存储库的origin分支设置为SSH

GitHub存储库设置页面只是一个建议的命令列表(GitHub现在建议使用HTTPS协议).除非您拥有对GitHub网站的管理访问权限,否则我不知道有任何方法可以更改其建议的命令.

如果您更愿意使用SSH协议,只需添加一个类似的远程分支(即使用此命令代替 GitHub的建议命令).要修改现有分支,请参阅下一节.

$ git remote add origin git@github.com:nikhilbhardwaj/abc.git
Run Code Online (Sandbox Code Playgroud)

修改预先存在的存储库

如您所知,要将预先存在的存储库切换为使用SSH而不是HTTPS,您可以更改.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
Run Code Online (Sandbox Code Playgroud)

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

$ git remote set-url origin git@github.com:nikhilbhardwaj/abc.git
Run Code Online (Sandbox Code Playgroud)

有关SSH-HTTPS开关的更多信息

  • 这可能对Windows用户有好处,但在Linux上却向后退了一步:ssh始终有效,而Smart HTTPS的新密码缓存仅适用于Windows.有关*"Mac版本在哪里?"的注释*但不是linux用户的**单**字. (3认同)

小智 157

这告诉git在连接到GitHub/BitBucket时总是使用SSH而不是HTTPS,因此默认情况下您将通过证书进行身份验证,而不是提示输入密码.

  • 由于这里的评论提到了自制程序问题,因此删除“--global”并在 pr 存储库的基础上执行此操作可能是个好主意。 (3认同)
  • 如果有人想在[文档](https://www.kernel.org/pub/software/scm/git/docs/git-config.html)中查找,请搜索`url.<base> .insteadOf `. (2认同)
  • 警惕这似乎打破了一些东西 - 我注意到我做了这个改变之后,自制软件的一些功能停止了工作(即安装非默认版本/分支) (2认同)
  • 我_认为_应该是 git config --global url.ssh://git@github.com:.insteadOf https://github.com/,因为 github 喜欢 git@github.com:&lt;USERNAME&gt;/&lt;REPO&gt; .git。(编辑`git config --global url.git@github.com:.insteadOf https://github.com/` 肯定适用于 git 2.7.4。) (2认同)

MoO*_*oOx 52

特雷弗提供回应是正确的.

但是这里有你可以直接添加到你的.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/
Run Code Online (Sandbox Code Playgroud)

  • 更简单的+1 (2认同)
  • 对于 Gitlab: `[url "ssh://git@gitlab.com/"]` `insteadOf = https://gitlab.com/` 如果您想影响推送 URL 但不影响获取,还有 `pushInsteadOf`。可以使用 `git remote -v` 来检查 git 将要使用的有效 URL。 (2认同)

rof*_*rol 11

您需要在 ssh 中而不是在 https 中克隆。

$ ssh-keygen -t ed25519 -C "your_email@example.com"
Run Code Online (Sandbox Code Playgroud)

将 的内容添加~/.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
Run Code Online (Sandbox Code Playgroud)

并像这样运行它

bash generate_ssh.sh your_email@example.com github.com
Run Code Online (Sandbox Code Playgroud)

更改您的远程网址

git remote set-url origin git@github.com:user/foo.git
Run Code Online (Sandbox Code Playgroud)

(或者只是编辑.git/config

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

检查连接

ssh -T git@github.com
Run Code Online (Sandbox Code Playgroud)


小智 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
Run Code Online (Sandbox Code Playgroud)

编辑 reponame/.git/config

[remote "origin"]
        url = git@github.com:username/repo.git
Run Code Online (Sandbox Code Playgroud)