这些天,当我在设置页面上的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
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)
小智 157
GitHub上
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
Run Code Online (Sandbox Code Playgroud)到位桶
git config --global url.ssh://git@bitbucket.org/.insteadOf https://bitbucket.org/
Run Code Online (Sandbox Code Playgroud)这告诉git在连接到GitHub/BitBucket时总是使用SSH而不是HTTPS,因此默认情况下您将通过证书进行身份验证,而不是提示输入密码.
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)
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)