我如何让git默认为ssh而不是新仓库的https

to94eoyn  于 2023-04-10  发布在  Git
关注(0)|答案(9)|浏览(120)

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

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

每当我必须推送提交时,我需要输入我的GitHub用户名和密码。
我可以手动把它改成

git@github.com:nikhilbhardwaj/abc.git

.git/config中。我觉得这很令人恼火-有没有什么方法可以将git配置为默认使用SSH?

68bkxrlz

68bkxrlz1#

将仓库的源分支设置为SSH

GitHub仓库设置页面只是一个建议的命令列表(GitHub现在建议使用HTTPS协议)。除非你有GitHub网站的管理权限,否则我不知道有什么方法可以更改他们建议的命令。
如果你更愿意使用SSH协议,只需像这样添加一个远程分支(即使用此命令 * 代替GitHub建议的命令 *)。要修改现有分支,请参阅下一节。

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

修改已有仓库

正如您已经知道的,要将预先存在的存储库切换为使用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

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

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

SSH-HTTPS交换机更多信息

vltsax25

vltsax252#

  • 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,因此默认情况下您将通过证书进行身份验证,而不是提示输入密码。

e4eetjau

e4eetjau3#

特雷弗提供的回答是正确的。
但以下是您可以直接添加到.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/
pokxtpni

pokxtpni4#

你需要在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
        Hostname $hostname *.$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

(or只需编辑.git/config
在www.example.com上将~/.ssh/github.com_rsa.pub的内容添加到您的ssh密钥github.com
检查连接

ssh -T git@github.com
rjjhvcjd

rjjhvcjd5#

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
3zwtqj6y

3zwtqj6y6#

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

4bbkushb

4bbkushb7#

FYI -我使用这个是因为github不再允许ssh:

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

ghhkc1vu8#

虽然这里的其他答案直接回答了关于自动将基于https的远程设备转换为git+ssh远程设备的问题(以一种我不知道可能的方式!直到git的新东西!),但从一开始就“正确”做到这一点的“正常”方法是不给予git提供https URL。
GitHub(沿着其他流行的git托管服务)总是有一个小按钮,可以让您获得git应该克隆的URL。您只需要单击小“SSH”按钮:

或者对于新项目

一旦你选择了“SSH”选项,GitHub(和其他人)会记住(只要你登录),并在未来将其设置为默认值。

lbsnaicq

lbsnaicq9#

如果你使用Gitlab

git remote -v

你可能会看到

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

只是取代国王,骑士,乡绅与自己的事情。骑士/乡绅只是我们的项目有不同的目录,然后你可以去

git remote set-url origin ssh://git@gitlab.king.com/knight/squire.git

git pull,或随便什么,享受你的天才

相关问题