我如何让git默认为ssh,而不是https的新存储库
现在,当我在设置页面上的GitHub上创build一个新的存储库时,我得到:
git remote add origin https://github.com/nikhilbhardwaj/abc.git git push -u origin master 而且每当我必须推送提交,我需要input我的GitHub用户名和密码。
我可以手动将其更改为
 git@github.com:nikhilbhardwaj/abc.git 
 在.git/config 。 我觉得这很让人讨厌 – 有什么办法可以configurationgit默认使用SSH吗? 
将存储库的原始分支设置为SSH
GitHub存储库设置页面只是一个build议的命令列表(现在GitHubbuild议使用HTTPS协议)。 除非你有GitHub网站的pipe理权限,否则我不知道有什么方法可以改变他们的build议命令。
如果你想使用SSH协议,只需像这样添加一个远程分支(即使用这个命令代替 GitHub的build议命令)。 要修改现有分支,请参阅下一节。
 $ 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交换机的更多信息
- “为什么Git总是要求我的密码?” – GitHub帮助页面。
- GitHub切换到智能HTTP – 相关的StackOverflow问题
- 手腕友好的Git用法的凭证caching – GitHub博客文章关于HTTPS,以及如何避免重新input密码
- 
GitHub上 git config --global url.ssh://git@github.com/.insteadOf https://github.com/
- 
到位桶 git config --global url.ssh://git@bitbucket.org/.insteadOf https://bitbucket.org/
这就告诉git在连接到GitHub / BitBucket时总是使用SSH而不是HTTPS,所以默认情况下你将通过证书进行身份validation,而不是被提示input密码。
Trevor提供的答复是正确的 。
 但是这里是你可以直接在你的.gitconfig添加的.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/ 
如果你想为不同的主机提供很多密钥,可以这样做:
创build一个脚本
 #!/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的内容添加到github.com上的ssh密钥中
检查连接
 ssh -T git@github.com 
确保在克隆时首先复制ssh链接,而不是https链接。 这是确保这种默认的PEBCAK方法;)