官方说明:https://help.github.com/articles/generating-ssh-keys/

1,为Github账户设置SSH key

文章地址:http://zuyunfei.com/2013/04/10/setup-github-ssh-key/

什么是SSH key

一直使用SSH连接服务器,但是对它的原理却不太了解。这次设置Octopress的时候,需要使用SSH 方式连接Github, 正好对SSH的工作方式做了下了解。(好像Github推荐使用HTTPS的方式访问repo, 以前Github受到过SSH密匙攻击,之后升级了SSH key的安全措施,https方式视乎更方便安全,不过Octopress的设置文档中,我并没有找到怎么使用HTTPS连接Github)

简单来说,SSH提供了两种级别的安全验证:

  1. 第一种级别是基于密码的安全验证,知道账号和密码,就可以登陆到远程主机。Team的开发工作中,就是使用这种方式登陆编译服务器,或者开发机器。因为是在内网中,这种级别的安全验证已经足够了。
  2. 第二种级别是基于Public-key cryptography (公开密匙加密)机制的安全验证,原理如下图所示:

其优点在于无需共享的通用密钥,解密的私钥不发往任何用户。即使公钥在网上被截获,如果没有与其匹配的私钥,也无法解密,所截获的公钥是没有任何用处的。

产生SSH key

根据Github提供的help文档,具体过程如下

1
2
$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory

使用ssh-keygen产生新的key

1
2
3
4
$ ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa):

使用默认的文件名直接enter, 按提示输入密码(如果不提供密码,SSH将无密码连接,如果private key泄露可能会有安全问题)

1
2
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

密匙产生成功

1
2
3
4
Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com 

上传public key到Github账户

  1. 登录github
  2. 点击右上方的Accounting settings图标
  3. 选择 SSH key
  4. 点击 Add SSH key

在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的~/.ssh/id_rsa.pub文件内容粘帖到key一栏,在点击“add key”按钮就可以了。
添加过程github会提示你输入一次你的github密码

设置SSH使用HTTPS的403端口

在局域网中SSH的22端口可能会被防火墙屏蔽,可以设置SSH使用HTTPS的403端口。

测试HTTPS端口是否可用

1
2
3
$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

编辑SSH配置文件 ~/.ssh/config 如下:

1
2
3
Host github.comHostname ssh.github.comPort 443

测试是否配置成功

1
2
3
$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

多个Github账号的SSH key切换

如果在一台机器上要登陆多个Github账户,需要一些配置,虽然现在并没有用到,但是先记下来以备不时之需,过程参看这里。

2,【GitHub】解决每次push代码到github都需要输入用户名和密码的方法

在github上,建立一个项目test,去主页查看可以看到

如果使用HTTPS:

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/guochy2012/test.git
git push -u origin master

Push an existing repository from the command line

git remote add origin https://github.com/guochy2012/test.git
git push -u origin master

如果采用SSH:

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:guochy2012/test.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:guochy2012/test.git
git push -u origin master

使用HTTPS需要每次输入密码,SSH则不用,但SSH需要配置密钥 。

关于怎么产生密钥可以参见《Generating SSH Keys》一文

3,github地址 从https改成ssh

打开命令行工具,运行 git remote set-url origin 例如:

1
2
3
4

$ git remote set-url origin git@github.com:user/repo.git

然后再次 commit,如果出现类似:

1
2
3
4

Permission denied (publickey).

字样,那么说明你的 SSH key 没有设置或已经失效(譬如升级到 Mountain Lion 系统后),请重新参照上文的官方文档进行设置即可。

4,执行pull时报错

wangkongming@AY140527171808170503Z:~/github/collect$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/wangkongming/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/wangkongming/.ssh/id_rsa
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

答案:http://stackoverflow.com/questions/1556119/ssh-private-key-permissions-using-git-gui-or-ssh-keygen-are-too-open

是因为给 id_rsa的权限太高了,改成700就可以了。也有人说600

You changed the permissions on the whole directory, which I agree with Splash is a bad idea. If you can remember what the original permissions for the directory are, I would try to set them back to that and then do the following

cd ~/.ssh
chmod 700 id_rsa

inside the .ssh folder. That will set the id_rsa file to rwx (read, write, execute) for the owner (you) only, and zero access for everyone else.

If you can't remember what the original settings are, add a new user and create a set of SSH keys for that user, thus creating a new .ssh folder which will have default permissions. You can use that new .ssh folder as the reference for permissions to reset your .ssh folder and files to.

If that doesn't work, I would try doing an uninstall of msysgit, deleting ALL .ssh folders on the computer (just for safe measure), then reinstalling msysgit with your desired settings and try starting over completely (though I think you told me you tried this already).

Edited: Also just found this link via Google -- Fixing "WARNING: UNPROTECTED PRIVATE KEY FILE!" on Linux While it's targeted at linux, it might help since we're talking liunx permissions and such.

======================================

以下是自己在使用git时,总结的:

1,查看当前项目远程分支的路径

git remote -v

转自:http://www.cnblogs.com/wangkongming/p/4158664.html

git提交代码到github gitbash相关推荐

  1. mac电脑Git提交代码到Github提示git-credential-osxkeychain 验证解决方案

    mac电脑Git提交代码到Github提示git-credential-osxkeychain 验证解决方案 参考文章: (1)mac电脑Git提交代码到Github提示git-credential- ...

  2. Git Bash基本命令,Git提交代码到GitHub,GitHub获取代码到本地

    Git简介,GitHub与Git区别,Git与SVN区别:Git简介 Git与GitHub区别和关系 Git官网下载(国内超慢):Git官网下载 Git淘宝镜像(下载非常快):Git淘宝镜像 推荐Gi ...

  3. 使用git提交代码到github仓库

    原文链接:http://www.cnblogs.com/specter45/p/github.html GitHub是基于git实现的代码托管.git是目前最好用的版本控制系统了,非常受欢迎,比之sv ...

  4. Android开发之git提交代码到GitHub仓库教程

    首先我们想要提交到GitHub要做一些前提工作. 我们得先在AS中登录GitHub账号 先ctrl+art+s打开设置然后输入GitHub账号,点击test,显示成功即可 接下来我们可以分享代码到仓库 ...

  5. 【Git】Git提交代码到GitHub的基本操作流程

    说明 本篇博客并非用于全程指导初学者入门Git,但我可以推荐一篇优秀的博文,是我入门所参考的:Here 本文适合于把基本配置做好并自己提交过一次的初学者,可以防止忘记命令或者走错流程. 操作流程 首先 ...

  6. git提交代码到github时出现everything up-to-date,但是代码没有上传成功

    上传后出现everything up-to-date 这时需要先在本地提交之后在进行push,步骤如下: 1.右键 2.出现以下界面提交 3.这时在push就可以成功了

  7. Git提交项目到GitHub

    一.GitHub新建项目 1.进入Github首页,点击New repository新建一个项目 2.填写相应信息后点击create即可 Repository name: 仓库名称 Descripti ...

  8. git提交代码到码云

    日常代码一般提交到github比较多,但我还是钟爱马爸爸,没错就是码云. 码云是中文版的代码托管的网站,不存在打开网速问题,使用也蛮方便的,日常自己保存托管代码已经足够,平时使用git提交代码到码云是 ...

  9. Git提交本地代码到GitHub

    提交代码到GitHub的基本流程: 1. 在GitHub上新建一个仓库. 2.找到项目所在文件夹,右键Git bash Here. 3. $ git init 4. $ git add .  (添加所 ...

最新文章

  1. 小马智行获2.67亿美元新融资,估值超53亿美元
  2. 全网最详细之一网打尽数据结构中与树相关的算法
  3. 影像组学视频学习笔记[44(End)]-带95%置信区间的折线图、Li‘s have a solution and plan.
  4. java 判断今天_Java 判断某个具体时间是否属于当天范围(24H)
  5. p中div -- a中a
  6. C#调用C++的dll文件方法
  7. svn移动目录时如何保留原来的日志
  8. 一步一步学习Servlet输出HelloServlet详解
  9. Java中@WebServlet的使用方法
  10. 蠕虫病毒通过什么侵入计算机系统,注意!蠕虫病毒入侵!我区已有单位个人计算机中招!...
  11. 第一个Net+Mysql的例子,比想象的简单很多
  12. keil软件安装与破解
  13. 换一种姿势阅读《人工智能简史》
  14. 如何在Linux中考硬盘数据,Linux硬盘文件数据粉碎
  15. 开发中常用的网址(快速解决方法)、免费的api接口地址、以及外包的接私活平台
  16. MYSQL默认隔离级别详解
  17. win7下安装配置theano详解
  18. array_diff()和array_diff_assoc()
  19. unity地形模块学习
  20. 【技术邻】基于Ansys Icepak的散热器优化

热门文章

  1. 如何识别哭泣csdn_你上一次流泪是什么时候?| 研究:几乎不哭泣的4类人
  2. C语言函数大全--h开头的函数
  3. 勾股定理(计算)C++
  4. 100寸大屏幕的影院观感 神画Q1智能影院了解一下
  5. 刻度标尺精确定位系统-更为人性化的位置检测系统
  6. Service not registered解决方案
  7. elastic-job之运维平台
  8. 联想天逸无法进入bios
  9. BT5R3下安装metasploit
  10. uniapp 刷新页面