git仓库相关知识02-远程仓库

一 。git远程仓库

    git常用命令一般用于操作本地仓库 git支持分布式开发 将本地仓库和远程仓库进行同步 互联网众多的git远程仓库如github,gitlab,码云,oschina云等用于开源代码分享

   远程操作的过程是:

           1 建立本地版本库和远程版本库的连接 (添加远程)

           2本地版本库发生变更时 可以推送变更数据到远程

           3远程仓库发生变更时 本地仓库提取到本地临时版本库 将临时版本库合并到当前版本库


二 。连接github 测试上传

   github支持远程代码托管 使用的是git仓库

管理仓库方式 一般使用git remote命令 如果只是普通的下载源代码 可以直接使用git clone命令  会将远程版本库代码同步到当前文件夹

git clone https://github.com/jiaozi789/cherry.git
管理仓库也可以使用本地的git软件管理一下介绍使用git命令同步本地版本库到远程版本库

需要直接登录操作远程git仓库 需要使用 ssh协议  ssh协议需要在github 上传本机的公钥

本机生成公私钥对(指定注册github的邮件地址 填错的也没关系 不过将来提交代码 在github会有个警告)

ssh-keygen -t rsa -C "你注册github的邮件"
如果忘记了邮件地址 查看注册账号的邮件地址:


生成的公私钥位于  ~/.ssh下

[root@node3 ~]# cd .ssh
[root@node3 .ssh]# ll
total 16
-rw-r--r-- 1 root root  784 Oct 17 19:28 authorized_keys
-rw------- 1 root root 1679 Oct 17 19:27 id_rsa
-rw-r--r-- 1 root root  392 Oct 17 19:27 id_rsa.pub
-rw-r--r-- 1 root root 1150 Nov  3 12:08 known_hosts
公钥文件名是 id_rsa.pub

查看公钥内容

[root@node3 .ssh]# more ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqEqhHN5r93K4WNVV9Lw6OceydWznjE7MAV6tWcI+Kq7oTi7GOK30H8FKu9nOvoc9/wZXTsCcQy2v8Nc7FWyt9DtMLspgRQ0RtJBxUMUlkky2pD7ps32MyZCIDv3AnIXc/SyvP7Q99yo1V0C4pnQqyGcOU5zL/Q7K3RM2TMgNUAtNjpQhqL0+iP1Tq4rFrhHPjWLWZo0DJUtkloSLHi+IiNHYX
XfJAx6zfqDb/Ny9iQDLanxrj35sIJ9rM+cMXK2SIHHyioNvH6Wc+M3+PR5DJXrBqtZSXQcOX+lJ04kjtkIYXy6YfRCmpN8KdWnTcLMlk3yY82oG+d3ezLVvRn/LB
将公钥内容添加到github具体过程参考下图



添加成功后 Linux执行ssh命令测试是否正常登录 

[root@node3 .ssh]# ssh -T git@github.com
The authenticity of host 'github.com (192.30.255.113)' 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)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Hi jiaozi789! You've successfully authenticated, but GitHub does not provide shell access.

我这里注册使用用户名 jiaozi789显示Hi jiaozi789

新建一个远程仓库 hellogit

创建完成后 github给出提交的一些脚本


上传文件到github测试 

 创建本地的版本库

[root@node3 git]# mkdir hellogit
[root@node3 git]# cd hellogit
[root@node3 hellogit]# git init
Initialized empty Git repository in /root/git/hellogit/.git/
[root@node3 hellogit]# echo hello>1.txt
[root@node3 hellogit]# git add 1.txt
[root@node3 hellogit]# git commit -m "测试git远程";
[master (root-commit) ff8a154] 测试git远程
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
[root@node3 hellogit]# git log
commit ff8a1547cf817dc0120f4065f574d13707ddea67
Author: jiaozi1 <you@example.com>
Date:   Mon Nov 6 17:08:00 2017 -0800

    测试git远程
添加一个远程仓库(远程取别名 这里是origin)

[root@node3 hellogit]# git remote add origin git@github.com:jiaozi789/hellogit.git
测试上传数据

[root@node3 hellogit]#  git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 224 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:jiaozi789/hellogit.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
刷新github看到hellogit多了一个 1.txt
三。远程仓库操作常用命令

1》添加远程

     远程仓库创建后  需要本地保存一个远程连接的信息(别名表示) 将来和远程操作就可以根据别名直接操作 不需要指定ip用户名密码等

git remote add 远程别名 远程地址

远程地址语法:git remote add origin git@github.com:用户名/仓库名.git

举例
[root@node3 test]# git remote add origin git@github.com:jiaozi789/test.git
[root@node3 test]# git remote  查看所有的远程信息列表
origin

2》提交远程

  必须是本地的版本库(不是暂存库)才能提交到远程仓库

git push -u 远程连接别名 需要提交本地版本库名称(默认是master)
案例参考 章节二


3》提取远程仓库
  github上远程上传一个文本文件


开始上传文件


抓取远程仓库改变到本地临时仓库(git fetch 远程别名)

[root@node3 hellogit]# git fetch origin       
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:jiaozi789/hellogit
   ff8a154..fa8fc31  master     -> origin/master  临时仓库是远程别名/master
合并到当前仓库

[root@node3 hellogit]# git merge origin/master
Updating ff8a154..fa8fc31
Fast-forward
 "345255246344271240350267257347272277.txt" | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 "345255246344271240350267257347272277.txt"
[root@node3 hellogit]# ll
total 8
-rw-r--r-- 1 root root  6 Nov  6 17:07 1.txt
-rw-r--r-- 1 root root 69 Nov  6 17:44 学习路线.txt
发现远程仓库的学习路线.txt同步到了本地仓库


4》删除远程仓库
  删除表示删除远程仓库的信息 如果需要删除远程仓库的文件 只需要删除本地仓库文件 push到远程仓库 远程仓库的
文件自然也就删除了

[root@node3 test]# git remote
origin
[root@node3 test]# git remote remove origin
[root@node3 test]# git remote












原文地址:https://www.cnblogs.com/liaomin416100569/p/9331171.html