如何让git默认使用ssh而不是https来访问新的软件仓库 [英] How do I get git to default to ssh and not https for new repositories

查看:281
本文介绍了如何让git默认使用ssh而不是https来访问新的软件仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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? h3>将存储库的源分支设置为SSH

GitHub存储库设置页面只是一个建议的命令列表(现在GitHub建议使用HTTPS协议)。除非您拥有GitHub网站的管理权限,否则我不知道有任何方法可以更改他们的建议命令。



如果您想使用SSH协议,只需添加像这样的远程分支(即使用GitHub建议命令的命令 )。要修改现有分支,请参阅下一节。

  $ git remote add origin git@github.com:nikhilbhardwaj / abc。 git 






修改预先存在的存储库



正如您已经知道的,要将预先存在的存储库切换为使用SSH而不是HTTPS,您可以在 .git / config file。

  [remoteorigin] 
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交换机的更多信息




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

And whenever I have to push a commit I need to enter my GitHub username and password.

I can manually change that to

git@github.com: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?

解决方案

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.

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


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

A shortcut is to use the set-url command:

$ git remote set-url origin git@github.com:nikhilbhardwaj/abc.git


More information about the SSH-HTTPS switch

这篇关于如何让git默认使用ssh而不是https来访问新的软件仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆