当我创建的设置页面,我得到关于新仓库,这些天:
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?
建立一个资料库的起源的分支是SSH
GitHub的仓库设置页面只是一个命令的建议名单(和GitHub上现在建议使用HTTPS协议)。 除非你有GitHub的网站管理权限,我不知道有什么办法改变他们的建议的命令。
如果你想用SSH协议,只需添加一个远程分支,像这样( 即地方 GitHub的使用这个命令提示命令)。 要修改现有的分支,请参阅下一节。
$ git remote add origin git@github.com:nikhilbhardwaj/abc.git
修改预先存在的库
正如你已经知道,切换预先存在仓库使用SSH而不是用于HTTPS,您可以在中更改远程URL .git/config
文件。
[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开关的详细信息
- “为什么总是Git的要求我的密码?” - GitHub的帮助页面。
- GitHub的开关,智能HTTP -相关的StackOverflow问题
- 在腕友好的Git使用凭据缓存 - GitHub的博客文章HTTPS,以及如何避免再次进入您的密码
这告诉混帐连接到GitHub上/到位桶时,总是使用SSH而不是HTTPS的,所以你会通过证书默认情况下,身份验证,而不是被提示输入密码。
在由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/
如果你想为不同的主机多钥匙,这样做:
创建一个脚本
#!/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
User git
IdentitiesOnly yes
IdentityFile $keypath
EOF
fi
并运行它像
sh script.sh myemail@example.com github.com
更改远程URL
git remote set-url origin git@github.com:user/foo.git
添加的〜/ .ssh / github.com_rsa.pub的内容到您的SSH密钥对github.com
检查连接
ssh -T git@github.com
您可能无意中克隆HTTPS而不是SSH存储库。 我犯了这个错误无数次在GitHub上。 请确保您复制ssh连接在首位克隆时,而不是HTTPS链接。