第六节《Git克隆》
本节学习如何使用git clone命令建立版本库克隆,以及如何使用git push和gitpull命令实现克隆之间的同步。
Git的版本库目录和工作区在一起,因此存在一损俱损的问题,即如果删除一个项目的工作区,同时也会把这个项目的版本库删除掉。一个项目仅在一个工作区中维护太危险,如果有两个工作区就会好很多。

图片中一个项目使用了两个版本库进行维护,两个版本库之间通过pull和push操作实现同步:
<1>版本库A通过克隆操作创建克隆版本库B
<2>版本库A可以通过push(推送)操作,将新提交传递给版本库B
<3>版本库A可以通过pull(拉回)操作,将版本库B中的新提交拉回到自身
<4>版本库B可以通过pull操作,将版本库A中的新提交拉回到自身
<5>版本库B可以通过push操作,将新提交传递给版本库A
Git使用git clone命令实现版本库克隆,主要有三种用法:
<1>git clone <repository> <directory>
<2>git clone --bare <repository> <director.git>
<3>git clone --mirror <repository> <director.git>
这三种用法的区别:
<1>用法一将<repository>指向的版本库创建一个克隆到<directory>目录。目录<directory>相当于克隆版本库的工作区,文件都会检出,版本库位于工作区下的.git目录中。
<2>用法二和用法三创建的版本库都不包含工作区,直接就是版本库的内容,这样的版本库称为裸版本库。一般约定俗成裸版本库的目录以.git为后缀。
<3>用法三区别于用法二之处在于用法三克隆出来的裸版本对上游版本库进行了注册,这样可以在裸版本库中使用git fetch命令和上游版本库进行持续同步。
对等工作区,不使用--bare或--mirror创建出来的克隆包含工作区,这样就会产生两个包含工作区的版本库,这两个版本库是对等的。

这两个工作区本质上没有区别,但是往往提交是在一个版本中进行的,另外一个作为备份。对于这种对等工作区模式,版本库的同步只有一种可行的操作模式,就是备份库执行git pull命令从源版本库中拉回新的提交实现版本库同步。为什么不能从版本库A向版本库B执行git push推送操作呢?看下面的操作。
<1>执行克隆命令
[root@git demo]# git clone /git/my/workspace/demo /git/my/workspace/demo-backup
Initialized empty Git repository in /git/my/workspace/demo-backup/.git/
<2>进入demo版本库,生成一些测试提交(使用--allow-empty参数可以生成空提交)
[root@git demo]# git commit --allow-empty -m "sync test 1"
[master 4f18802] sync test 1
[root@git demo]# git commit --allow-empty -m "sync test 2"
[master e8d59b4] sync test 2
<3>能够在demo版本库向demo-backup版本库执行push操作吗?
[root@git demo]# git push /git/my/workspace/demo-backup/
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 270 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /git/my/workspace/demo-backup/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/git/my/workspace/demo-backup/'
<4>为了实现同步,需要进入到备份库中执行git pull操作
[root@git demo]# cd /git/my/workspace/demo-backup/
[root@git demo-backup]# git pull
From /git/my/workspace/demo
06e5df8..e8d59b4 master -> origin/master
Updating 06e5df8..e8d59b4
Fast-forward
[root@git demo-backup]# git log --oneline -2
e8d59b4 sync test 2
4f18802 sync test 1
下面介绍下裸版本库,裸版本库不但可以通过克隆的方式创建,还可以通过git init命令以初始化的方式创建,之后的同步和上面的同步方式大同小异。

命令git init是用于初始化一个版本库的,之前执行git init命令初始化的版本库是带工作区的,如何以裸版本库的方式初始化一个版本库呢?答案是使用--bare参数。
<1>创建一个裸版本仓库。
[root@git repos]# git init --bare demo-init.git
Initialized empty Git repository in /path/to/repos/demo-init.git/
<2>查看一下demo-init.git下的内容
[root@git repos]# ls -F demo-init.git/
branches/ config description HEAD hooks/ info/ objects/ refs/
<3>查看一下这个版本库的配置core.bare的值
[root@git repos]# git --git-dir=/path/to/repos/demo-init.git config core.bare
true
<4>向裸版本库中推送数据,进入源版本库中执行操作
[root@git demo]# git push /path/to/repos/demo-init.git master:master
Counting objects: 21, done.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (21/21), 1.90 KiB, done.
Total 21 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (21/21), done.
To /path/to/repos/demo-init.git
* [new branch] master -> master
<5>查看一下demo-init.git版本库中的提交
[root@git demo]# git --git-dir=/path/to/repos/demo-init.git log --oneline -2
e8d59b4 sync test 2
4f18802 sync test 1
<6>继续在demo中执行几次提交,再向demo-init.git中推送
[root@git demo]# git commit --allow-empty -m "sync test 5"
[master 2007420] sync test 5
[root@git demo]# git commit --allow-empty -m "sync test 6"
[master 49f01b9] sync test 6
[root@git demo]# git push /path/to/repos/demo-init.git
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /path/to/repos/demo-init.git
e8d59b4..49f01b9 master -> master
为什么这次使用git push命令后面没有跟上分支名呢?这是因为远程版本库中已经不再是空版本库了,有名为master的分支,通过以下命令查看远程版本库分支
[root@git demo]# git ls-remote /path/to/repos/demo-init.git
49f01b9d4d7a4c558def93fcbe88f8e3137b5b6f HEAD
49f01b9d4d7a4c558def93fcbe88f8e3137b5b6f refs/heads/master
第六节《Git克隆》的更多相关文章
- git第六节---git 远程仓库
远程分支类似于本地分支,是指向远程仓库中的文件的指针. 1.远程分支抓取 @git fetch origin dev :拉取远程dev内容 fetch不会对本地仓库内容进行更新,只更新远端commit ...
- Tigase-02 tigase-server7.1.0使用git 克隆下来,并在eclipse 上运行调试
继 Tigase-01 使用spark或spi登录Tigase服务器,这节说明下使用 eclipse git克隆 tigase-server7.1.0,并运行调试!最近有不少同学尝试去git clon ...
- VS2013 GIT 克隆远程仓库
1.配置本地GIT 工具->选项->源代码管理,选择GIT 2.打开团队资源管理器,找到GIT克隆选项 3.单击克隆,在输入框内输入远程仓库地址,然后单击克隆即可 GIT 插件配置:参考 ...
- android 入门-android Studio git 克隆
最后是完成 以上是如何从android studio Git 克隆Github的项目
- 基于Extjs的web表单设计器 第六节——界面框架设计
基于Extjs的web表单设计器 基于Extjs的web表单设计器 第一节 基于Extjs的web表单设计器 第二节——表单控件设计 基于Extjs的web表单设计器 第三节——控件拖放 基于Extj ...
- git 克隆项目 与 分支简单操作
git clone http://abcde.com/myproject/abc.git 克隆远程项目到本地githome文件夹git branch -a 查看所有分支 包括远程和本地 *号开头表示当 ...
- 1. Git 克隆代码
1. Git 克隆代码 git clone git://github.com/facebook/hiphop-php.git 2. Git更新分支 查看服务器上的所有分支 [huzg@slave3 h ...
- 第一百二十六节,JavaScript,XPath操作xml节点
第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...
- VUE2.0实现购物车和地址选配功能学习第六节
第六节 地址列表过滤和展开所有的地址 html:<li v-for="(item,index) in filterAddress">js: new Vue({ el:' ...
- delphi 线程教学第六节:TList与泛型
第六节: TList 与泛型 TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...
随机推荐
- 用vector与bitset分别创建1亿以内的素数表,比较快慢
vector容器: 代码如下: #include<iostream>#include<vector>#include<ctime>using namespace s ...
- java——形参与实参
看了很多的文章,稍微有一些的总结:对最基本的形参与实参有了一定的理解,虽然还是不够深入. 1.基本概念 形参:全称为"形式参数"是在定义函数名和函数体的时候使用的参数,目的是用来接 ...
- element-项目用到偏门方法~
开发项目的时候,组件库的使用有时会为我们节省开发时间,提高开发效率,但组件库样式有时与我们的设计图出入很大,还有的方法也很偏门,主要官方文档有时候对于一些方法和属性介绍的也比较少,以下是我在工作中总结 ...
- [.NET] 使用ValidationContext快速进行模型资料的验证
在进行WebAPI功能开发的时候,一般传统的验证资料是否合法的方式,都是透过if/else的方式进行判断若是使用ValidationContext,就可以省去很多自行撰写程式码的工作 要使用Valid ...
- OpenGL之shader着色器的应用,三色渐变的三角形
学习自: https://learnopengl-cn.github.io/01%20Getting%20started/05%20Shaders/#_7 首先放一张效果图: 本次教程,将着色器单独定 ...
- [模板] 无旋Treap (C++ class)
注意!本帖不是算法介绍!只是贴代码(逃) //嫌stdlib的rand太慢,手打了一个 /* Author: hotwords */ typedef unsigned int tkey; class ...
- Hadoop学习------Hadoop安装方式之(二):伪分布部署
要想发挥Hadoop分布式.并行处理的优势,还须以分布式模式来部署运行Hadoop.单机模式是指Hadoop在单个节点上以单个进程的方式运行,伪分布模式是指在单个节点上运行NameNode.DataN ...
- 宝塔MySQL服务自动停止重启的解决方法
现象:客户端MYSQL无法链接 提示超过 max_connections 如果重新启动MYSQL或停止MYSQL 及重新启动系统时 需要很长时间 1个小进左右 问题描述 服务器上安装的 MySQL,会 ...
- Python 锁 同步 互斥锁
import time from threading import Lock,Thread num = 100 def f1(loc): loc.acquire() global num tmp = ...
- [工作日志] 2019-04-16 入参是list
入参 {"vehicleList":[{"vehicleNo":"赣K81057","plateColor":" ...