对于新存储库,如何让 git 默认为 ssh 而不是 https [英] How do I get git to default to ssh and not https for new repositories
问题描述
这些天,当我在 GitHub 上的设置页面上创建一个新存储库时:
These days when I create a new repository on GitHub on the setup page I get:
git remote add origin https://github.com/nikhilbhardwaj/abc.git
git push -u origin master
每当我必须推送提交时,我都需要输入我的 GitHub 用户名和密码.
And whenever I have to push a commit I need to enter my GitHub username and password.
我可以手动将其更改为
git@github.com:nikhilbhardwaj/abc.git
在 .git/config
中.我觉得这很烦人 - 有什么方法可以将 git 配置为默认使用 SSH?
in the .git/config
. I find this quite irritating - is there some way I can configure git to use SSH by default?
推荐答案
将仓库的源分支设置为 SSH
GitHub 存储库设置页面只是建议的命令列表(GitHub 现在建议使用 HTTPS 协议).除非您拥有 GitHub 站点的管理权限,否则我不知道有什么方法可以更改他们建议的命令.
Set up a repository's origin branch to be 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.
如果您更愿意使用 SSH 协议,只需像这样添加一个远程分支(即使用此命令代替 GitHub 建议的命令).要修改现有分支,请参阅下一节.
If you'd rather use the SSH protocol, simply add a remote branch like so (i.e. use this command in place of GitHub's suggested command). To modify an existing branch, see the next section.
$ git remote add origin git@github.com:nikhilbhardwaj/abc.git
<小时>
修改预先存在的存储库
如您所知,要将预先存在的存储库切换为使用 SSH 而不是 HTTPS,您可以更改 .git/config
文件中的远程 URL.
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.
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
-url = https://github.com/nikhilbhardwaj/abc.git
+url = git@github.com:nikhilbhardwaj/abc.git
一个快捷方式是使用 set-url
命令:
A shortcut is to use the set-url
command:
$ git remote set-url origin git@github.com:nikhilbhardwaj/abc.git
<小时>
有关 SSH-HTTPS 切换的更多信息
- "为什么 Git 总是要求我的密码?" - GitHub 帮助页面.
- GitHub 切换到 Smart HTTP - 相关 StackOverflow 问题
- 用于手腕友好的 Git 使用的凭据缓存 - 关于 HTTPS 以及如何避免重新输入密码的 GitHub 博客文章
- "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
More information about the SSH-HTTPS switch
这篇关于对于新存储库,如何让 git 默认为 ssh 而不是 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!