github设置添加SSH

https 和 SSH 的区别:

1、前者可以随意克隆github上的项目,而不管是谁的;而后者则是你必须是你要克隆的项目的拥有者或管理员,且需要先添加 SSH key ,否则无法克隆。

2、https url 在push的时候是需要验证用户名和密码的;而 SSH 在push的时候,是不需要输入用户名的,如果配置SSH key的时候设置了密码,则需要输入密码的,否则直接是不需要输入密码的。

 

 

 

在 github 上添加 SSH key 的步骤:

1、首先需要检查你电脑是否已经有 SSH key 

 

$ cd ~/.ssh
$ ls

 

2、创建一个 SSH key 

$ ssh-keygen -t rsa -C "your_email@example.com"

-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字,比如邮箱。
-f 指定密钥文件存储文件名。

以上代码省略了 -f 参数,因此,运行上面那条命令后会让你输入一个文件名,用于保存刚才生成的 SSH key 代码,如:

Generating public/private rsa key pair.
# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]

 

当然,你也可以不输入密码,直接按回车。那么push的时候就不需要输入密码,直接提交到github上了,如:

Enter passphrase (empty for no passphrase): 
# Enter same passphrase again:

 

Your identification has been saved in /c/Users/you/.ssh/id_rsa.
# Your public key has been saved in /c/Users/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

 

a、首先你需要拷贝 id_rsa.pub 文件的内容,你可以用编辑器打开文件复制,也可以用git命令复制该文件的内容,如:

 

$ clip < ~/.ssh/id_rsa.pub

b、登录你的github账号,从又上角的设置( Account Settings )进入,然后点击菜单栏的 SSH key 进入页面添加 SSH key。

c、点击 Add SSH key 按钮添加一个 SSH key 。把你复制的 SSH key 代码粘贴到 key 所对应的输入框中,记得 SSH key 代码的前后不要留有空格或者回车。当然,上面的 Title 所对应的输入框你也可以输入一个该 SSH key 显示在 github 上的一个别名。默认的会使用你的邮件名称。

 

在git Bash 中输入以下代码

 

$ ssh -T git@github.com

 

The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?

 

Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':

注意:输入密码时如果输错一个字就会不正确,使用删除键是无法更正的。

 

Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.

access denied” ,者表示拒绝访问,那么你就需要使用 https 去访问,而不是 SSH 。

 

git常用命令

在github windows工具中使用Git Bash打开项目,使用cd命令进入已有项目根目录下

 

touch README.md //新建说明文件
git init //在当前项目目录中生成本地git管理,并建立一个隐藏.git目录
git add . //添加当前目录中的所有文件到索引
git commit -m "first commit" //提交到本地源码库,并附加提交注释
git remote add origin https://github.com/chape/test.git //添加到远程项目,别名为origin
git push -u origin master //把本地源码库push到github 别名为origin的远程项目中,确认提交

提交完成,查看repository。

 

更新代码

cd /d/TVCloud
git add .
git commit -m "update test" //检测文件改动并附加提交注释
git push -u origin master //提交修改到项目主线

 

github常用命令

git push origin master //把本地源码库push到Github上
git pull origin master //从Github上pull到本地源码库
git config --list //查看配置信息
git status //查看项目状态信息
git branch //查看项目分支
git checkout -b host//添加一个名为host的分支
git checkout master //切换到主干
git merge host //合并分支host到主干
git branch -d host //删除分支host
git log --graph //查看分支情况
posted @ 2016-10-26 16:28  小凯全栈开发  阅读(229)  评论(0编辑  收藏  举报