您的位置:首页 > 其它

【Git学习笔记】远程仓库

2016-12-14 17:18 330 查看

第一种情景:本地初始化一个Git仓库后,接着又在github上创建了一个Git仓库,现在要让这两个仓库进行远程同步。

1. 关联本地仓库就和远程仓库 $ git remote add origin git@code.csdn.net:LHAT_7/javaee_project.git

2. 然后把本地仓库的所有内容推送到远程仓库中,正常情况如下:

$ git push -u origin master
Counting objects: 4153, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3574/3574), done.
Writing objects: 100% (4153/4153), 17.74 MiB | 1.98 MiB/s, done.
Total 4153 (delta 1224), reused 0 (delta 0)
To git@code.csdn.net:LHAT_7/javaee_project.git
05d9e67..c384948  master -> master
Branch master set up to track remote branch master from origin.


由于远程库是空的,我们第一次推送
master
分支时,加上了 -u 参数,Git不但会把本地的
master
分支内容推送的远程新的
master
分支,还会把本地的
master
分支和远程的
master
分支关联起来,在以后的推送或者拉取时就可以简化命令。

出错的情况如下

$ git push -u orgin master
fatal: 'orgin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


解决方法:

1. $ git remote remove origin

2. $ git remote add origin git@code.csdn.net:LHAT_7/javaee_project.git

对的,我的理解就是重新关联一下就ok了。

也可以去这个地址详细的看http://stackoverflow.com/questions/32238616/git-push-fatal-origin-does-not-appear-to-be-a-git-repository-fatal-could-n/32238628

  

第二种情景:本地没有Git仓库,要从远程仓库克隆。

$ git clone git@github.com:iwanttodo/java.git
Cloning into 'java'...
remote: Counting objects: 24, done.
remote: Total 24 (delta 0), reused 0 (delta 0), pack-reused 24
Receiving objects: 100% (24/24), 9.91 KiB | 0 bytes/s, done.
Resolving deltas: 100% (2/2), done.
Checking connectivity... done.


SSH

公钥是远程仓库识别您的用户身份的一种认证方式,通过公钥,您可以将本地Git仓库与远程仓库建立联系,然后您就可以很方便的将本地代码上传到远程仓库,或者将远程仓库代码下载到本地了,而不用每次都要输入用户名和密码了。

前提:安装git

1. 首先生成密钥 $ ssh-keygen -t rsa -C "your email" ,然后一路回车。

$ cd ~/.ssh
$ ls -l
total 2
-rw-r--r-- 1 liuchuanwei 197121 1675 12月 14 19:48 id_rsa
-rw-r--r-- 1 liuchuanwei 197121  399 12月 14 20:39 id_rsa.pub


在 ./ssh 目录下会生成 id_rsa 和 id_rsa.pub 两个密钥,前者是本地用的,后者是远程用即公钥。

2. 接着进入 github(或者 code.csdn.net等等),创建一个公钥,名称title任意,密钥key填写 id_rsa.pub 的内容(注意:不要带多余的空格)

3. 测试是否成功

$ ssh git@github.com
//正常情况下,回显如下
PTY allocation request failed on channel 0
Hi plinx! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.


为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。

当然,GitHub允许你添加多个Key。假定你有若干电脑,你一会儿在公司提交,一会儿在家里提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: