对于新存储库,如何让 git 默认为 ssh 而不是 https
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11200237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get git to default to ssh and not https for new repositories
提问by nikhil
These days when I create a new repository on GitHub on the setup page I get:
这些天,当我在 GitHub 上的设置页面上创建一个新存储库时,我得到:
git remote add origin https://github.com/nikhilbhardwaj/abc.git
git push -u origin master
And whenever I have to push a commit I need to enter my GitHub username and password.
每当我必须推送提交时,我都需要输入我的 GitHub 用户名和密码。
I can manually change that to
我可以手动将其更改为
[email protected]:nikhilbhardwaj/abc.git
in the .git/config
. I find this quite irritating - is there some way I can configure git to use SSH by default?
在.git/config
. 我觉得这很烦人 -有什么方法可以将 git 配置为默认使用 SSH?
回答by David Cain
Set up a repository's origin branch to be SSH
将存储库的原始分支设置为 SSH
The GitHub repository setup page is just a suggested list of commands (and GitHub now suggests using the HTTPS protocol). Unless you have administrative access to GitHub's site, I don't know of any way to change their suggested commands.
GitHub 存储库设置页面只是一个建议的命令列表(GitHub 现在建议使用 HTTPS 协议)。除非您对 GitHub 的站点具有管理访问权限,否则我不知道有什么方法可以更改他们建议的命令。
If you'd rather use the SSH protocol, simply add a remote branch like so (i.e. use this command in placeof GitHub's suggested command). To modify an existing branch, see the next section.
如果您更愿意使用 SSH 协议,只需像这样添加一个远程分支(即使用此命令代替GitHub 建议的命令)。要修改现有分支,请参阅下一节。
$ git remote add origin [email protected]:nikhilbhardwaj/abc.git
Modify a pre-existing repository
修改预先存在的存储库
As you already know, to switch a pre-existing repository to use SSH instead of HTTPS, you can change the remote url within your .git/config
file.
如您所知,要将预先存在的存储库切换为使用 SSH 而不是 HTTPS,您可以更改.git/config
文件中的远程 url 。
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
-url = https://github.com/nikhilbhardwaj/abc.git
+url = [email protected]:nikhilbhardwaj/abc.git
A shortcut is to use the set-url
command:
一个快捷方式是使用以下set-url
命令:
$ git remote set-url origin [email protected]:nikhilbhardwaj/abc.git
More information about the SSH-HTTPS switch
有关 SSH-HTTPS 开关的更多信息
- "Why is Git always asking for my password?"- GitHub help page.
- GitHub's switch to Smart HTTP- relevant StackOverflow question
- Credential Caching for Wrist-Friendly Git Usage- GitHub blog post about HTTPS, and how to avoid re-entering your password
- “为什么 Git 总是要求我输入密码?” - GitHub 帮助页面。
- GitHub 切换到 Smart HTTP- 相关的 StackOverflow 问题
- 用于手腕友好 Git 使用的凭证缓存- 关于 HTTPS 以及如何避免重新输入密码的 GitHub 博客文章
回答by Trevor Austin
GitHub
git config --global url.ssh://[email protected]/.insteadOf https://github.com/
BitBucket
git config --global url.ssh://[email protected]/.insteadOf https://bitbucket.org/
GitHub
git config --global url.ssh://[email protected]/.insteadOf https://github.com/
比特桶
git config --global url.ssh://[email protected]/.insteadOf https://bitbucket.org/
That tells git to always use SSH instead of HTTPS when connecting to GitHub/BitBucket, so you'll authenticate by certificate by default, instead of being prompted for a password.
这告诉 git 在连接到 GitHub/BitBucket 时始终使用 SSH 而不是 HTTPS,因此默认情况下您将通过证书进行身份验证,而不是被提示输入密码。
回答by MoOx
The response provided by Trevor is correct.
But here is what you can directly add in your .gitconfig
:
但这是您可以直接添加到您的.gitconfig
:
# Enforce SSH
[url "ssh://[email protected]/"]
insteadOf = https://github.com/
[url "ssh://[email protected]/"]
insteadOf = https://gitlab.com/
[url "ssh://[email protected]/"]
insteadOf = https://bitbucket.org/
回答by Mike Lyons
You may have accidentally cloned the repository in https instead of ssh. I've made this mistake numerous times on github. Make sure that you copy the ssh link in the first place when cloning, instead of the https link.
您可能不小心在 https 而不是 ssh 中克隆了存储库。我在github上多次犯过这个错误。确保在克隆时首先复制 ssh 链接,而不是 https 链接。
回答by rofrol
You need to clone in ssh not in https.
您需要在 ssh 中而不是在 https 中进行克隆。
For that you need to set your ssh keys. I have prepared this little script that automates this:
为此,您需要设置 ssh 密钥。我准备了这个自动化的小脚本:
#!/usr/bin/env bash
email=""
hostname=""
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 *.$hostname
User git
IdentitiesOnly yes
IdentityFile $keypath
EOF
fi
and run it like
并运行它
bash script.sh [email protected] github.com
Change your remote url
更改您的远程网址
git remote set-url origin [email protected]:user/foo.git
Add content of ~/.ssh/github.com_rsa.pub
to your ssh keys on github.com
将 的内容添加~/.ssh/github.com_rsa.pub
到 github.com 上的 ssh 密钥
Check connection
检查连接
ssh -T [email protected]
回答by bhargav joshi
SSH File
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
Edit reponame/.git/config
编辑 reponame/.git/config
[remote "origin"]
url = [email protected]:username/repo.git