您的位置:首页 > 编程语言

windows下git的安装、配置与操作(六)——远程仓库(建立与github的联系)

2014-11-14 14:55 615 查看
一、注册github的账号并且创建一个工程。github

二、创建秘钥:

由于你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的所以他们之间的联系需要密钥即口令。

首先我们检查本机有没有秘钥:

$ cd ~/. ssh 检查本机的ssh密钥


如果不是第一次使用,请执行下面的操作,清理原有ssh密钥。

$ mkdir key_backup
$ cp id_rsa* key_backup
$ rm id_rsa*


如果没有:
$ ssh-keygen -t rsa -C "youremail@example.com"
[/code]
一直回车直至生成完成。我们的秘钥就生成好了。
三、设置github SSH key

登录github在用户设置里边找到SSH keys设置。



title填写 key是在~/用户/.ssh/id_rsa.pub文件中的内容

$ ssh -T git@github.com
Hi ZhangLeiStar! You've successfully authenticated, but GitHub does not provide
shell access.
这样表示我们连接成功了。

然后我们本地版本库与github版本库进行连接:
$ git remote add origin git@github.com:ZhangLeiStar/MyTestApp1.git
接下来就可以将本地库push到服务器上。这里需要注意一个问题。如果github上的repository不是空的话。使用push命令会报错:

$ git push -u origin master
To git@github.com:ZhangLeiStar/MyTestApp1.git
! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:ZhangLeiStar/MyTestApp1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


意思是远程服务器上有我们的工作。不能被覆盖。这时候我们需要先pull一下,将服务器上的工作拉下来:

$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:ZhangLeiStar/MyTestApp1
* branch            master     -> FETCH_HEAD
* [new branch]      master     -> origin/master
Merge made by the 'recursive' strategy.
README.md | 4 ++++
1 file changed, 4 insertions(+)
create mode 100644 README.md


然后在push:

$ git push -u origin master
Counting objects: 24, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (23/23), 2.11 KiB | 0 bytes/s, done.
Total 23 (delta 3), reused 0 (delta 0)
To git@github.com:ZhangLeiStar/MyTestApp1.git
76ce8c8..e5ff9c1  master -> master
Branch master set up to track remote branch master from origin.


第一次使用加参数-u以后可以不加。这样我们的服务器目录如下图:



这样我们就完成了版本库与服务器的联系。
如果我们是现在服务器上建立了一个repository:那我们就要用clone指令来克隆下来:

$ git clone git@github.com:ZhangLeiStar/MyTestApp2.git
Cloning into 'MyTestApp2'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.

$ cd MyTestApp2

$ ls
README.md
如果有多个人协作开发,那么每个人各自从远程克隆一份就可以了。 如果需要clone开源的工程。先在github中fork到自己的repository中。然后用上面的指令clone下来。

GitHub给出的地址不止一个,还可以用
https://github.com/ZhangLeiStar/
MyTestApp2
.git
这样的地址。实际上,Git支持多种协议,默认的
git://
使用ssh,但也可以使用
https
等其他协议。使用
https
除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用
ssh
协议而只能用
https
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git github 推送与克隆