打赏
Fork me on GitHub

[原]git的使用(六)---远程仓库

10.远程仓库

---------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------

环境:使用自己搭建的gitlab远程仓库,如何搭建仓库?请点击搭建远程仓库gitlab

本地仓库端:(windows下Git Bash)

  在本地创建ssh密钥对

$ ssh-keygen -t rsa -C "lhb@lhb.com"      #创建ssh密钥对,一路enter
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
/c/Users/Administrator/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:X98ep2jnIQ0Rn/sPekQysligGFqWQo36xXLuJh7g4Ro lhb@lhb.com
The key's randomart image is:
+---[RSA 2048]----+
| ..o .      .    |
|  o *   .    o . |
| . * o . .  . o  |
|. o = .   o o... |
|.o =    So oo+.  |
|o.o .   .... +.o |
|Eo..      . ..=.+|
|....o       .+o=+|
|...o       .o+o o|
+----[SHA256]-----+

 

查看创建的密钥对:

$ cd ~                                   #进入到用户主目录
$ pwd                                    #创建的密钥对的路径
/c/Users/Administrator

$ ls -al
total 5699
drwxr-xr-x 1 Administrator 197121       0 3月  27 06:54 ./
drwxr-xr-x 1 Administrator 197121       0 1月  25 10:55 ../
drwxr-xr-x 1 Administrator 197121       0 2月   4 23:56 .android/
drwxr-xr-x 1 Administrator 197121       0 3月  23 09:02 .eclipse/
-rw-r--r-- 1 Administrator 197121      69 3月  27 06:54 .gitconfig
drwxr-xr-x 1 Administrator 197121       0 3月  23 09:03 .m2/
drwxr-xr-x 1 Administrator 197121       0 2月   1 13:43 .oracle_jre_usage/
drwxr-xr-x 1 Administrator 197121       0 3月  24 01:27 .p2/
drwxr-xr-x 1 Administrator 197121       0 3月  26 06:17 .ssh/                    #创建的.ssh密钥对目录


$ cd .ssh #进入.ssh密钥对目录

$ ls #查看密钥对
id_rsa  id_rsa.pub  known_hosts #将id_rsa.pub中的内容拷贝,用于粘贴到远程仓库ssh密钥对中。

在远程仓库web界面中粘贴密钥对

创建远程仓库:

 

 

远程仓库建立好之后 在仓库建立的下面有一排提示:

意思是将本地的仓库上传到远程仓库的操作。

-----------------------------------------------------------准备开始上传本地仓库到远程仓库-----------------------------------

$ git remote add origin git@ubuntu:lhb/demo.git              #在本地的git仓库登记远程仓库名origin和地址git@ubuntu:lhb/demo.git


$ git push -u origin master                                  #将本地仓库的代码推送到 远程仓库(注意!注意!注意!第一次推送都要加上 -u)
The authenticity of host 'ubuntu (192.168.123.128)' can't be established.
ECDSA key fingerprint is SHA256:+Td4IWXk0C4R9jTZLi4u6az0FQ74EFZGNVETzKzYtL8.
Are you sure you want to continue connecting (yes/no)? yes                     #因为使用的是ssh协议,第一次要验证gitlab服务器的指纹
Warning: Permanently added 'ubuntu,192.168.123.128' (ECDSA) to the list of known hosts.  #提示:已经将gitlab服务器加入到一个可信任列表
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (19/19), 1.47 KiB | 0 bytes/s, done.
Total 19 (delta 4), reused 0 (delta 0)
To git@ubuntu:lhb/demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

----------只要在本地注册了远程仓库-----------
以后推送就用
git push origin master

为什么推送本地仓库到远程仓库要加一个参数 -u 以后推送就不用加了呢?

###########################################################################################
git push -u origin master   [origin表示远程仓库  master代表本地仓库的master分支]
原意是:git push master            推送本地仓库的mater分支,但是推送到哪没有说
    git push origin master     推送本地仓库的mater分支到origin分支
    git push -u origin master  推送本地仓库的mater分支到origin分支,并在本地仓库中注册远程仓库origin       -u = up-stream
肯定会问
注册的地方在哪呢?
在本地仓库有个config文件内
[remote "origin"]
    url = git@ubuntu:lhb/demo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
##############################################################################################

从远程仓库中克隆project到本地:

要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。

Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。

git clone  git@ubuntu:lhb/demo.git

 -----------------------------------在push本地仓库到远程仓库的时候的问题-------------------------------------

GIT问题,error:src refspec master does not match any

$ git push -u origin2 master
error: src refspec master does not match any.
error: failed to push some refs to 'git@ubuntu:lhb/quick4j.git'

需要知道的理论: 本地版本库为空, 空目录不能提交 (只进行了init, 没有add和commit)

解决方案:

$ git add -f *       #将所有的文件都提交到暂存区

$ git  commit -m "add quick4j project"   #将暂存区的文档提交到.git本地仓库

$ git push -u origin2 master #提交代码到远程仓库,之前给远程仓库取得名字是origin2
Counting objects: 4262, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4017/4017), done.
Writing objects: 100% (4262/4262), 80.23 MiB | 6.67 MiB/s, done.
Total 4262 (delta 1394), reused 0 (delta 0)
To git@ubuntu:lhb/quick4j.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin2.

 

 参考git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

 

  

posted @ 2016-03-27 17:17  my_cool2007  阅读(549)  评论(0编辑  收藏  举报