首页 技术 正文
技术 2022年11月9日
0 收藏 334 点赞 3,129 浏览 3503 个字

1.刚创建好的空仓库的分支是空的,即使是master分支也是不存在的。master分支是不能通过git branch 来创建的,只有在完成第一次提交才会自动创建,有git自动完成master分子的创建,也就是只有第一次提交创建好master分支后,才能再创建别的分支。因为git本质上就是基于图论原理的,图的第一个起点是系统在第一次提交的时候自动创建的,别的创建的所有的都是其他的分支都是第一个master分支后的“分支”。master作为主分支,在所有的git项目都是固定。所有的git项目都有master主分支,分分支则是不确定的。

harvey@harvey-Virtual-Machine:~/demo3$ git init ../demo4 #创建一个新的空仓库
Initialized empty Git repository in /home/harvey/demo4/.git/
harvey@harvey-Virtual-Machine:~/demo3$ cd ../demo4
harvey@harvey-Virtual-Machine:~/demo4$ echo "fsf">tes #创建新文件t
harvey@harvey-Virtual-Machine:~/demo4$ git add ./ #添加到缓存区
harvey@harvey-Virtual-Machine:~/demo4$ git branch#打印branch 列表发现为空,即使master分支也不存在
harvey@harvey-Virtual-Machine:~/demo4$ git branch master#手动创建master分支失败
fatal: Not a valid object name: 'master'.
harvey@harvey-Virtual-Machine:~/demo4$ git branch 分支#手动创建"分支"分支失败
fatal: Not a valid object name: 'master'.
harvey@harvey-Virtual-Machine:~/demo4$ git commit ./#第一次提交commit(隐式的创建master分支)
[master (root-commit) eca4197] sfsf:wq
1 file changed, 1 insertion(+)
create mode 100644 test
harvey@harvey-Virtual-Machine:~/demo4$ git branch#打印master分支发现分支创建成功
* master
harvey@harvey-Virtual-Machine:~/demo4$ git branch 分支#再次创建“分支”分支
harvey@harvey-Virtual-Machine:~/demo4$ git branch#再打印branch发现列表有数据了
* master
分支

2.没有完成第一次提交也就是没有创建master分支的时候,是不能checkout master的因为没有master分支存在。这个时候,只能使用的是checkout . 命令,因为checkout .的意思就是把缓存区的数据覆盖工作空间.在没有创建提交的时候,可以用点代表全部的缓冲区文件,也可以用git checkout –<file>检出单独的文件。可以认为缓冲区stage就是也个目录,我们的checkout就是从stage这个目录把数据拷贝到当前工作的目录。而commit就是把stage这个文件夹再做单独的备份,备份从原理上也是在master这个主文件下,如果还是单个的子文件夹就是单独分支,如果在master后创建了新的分支就如同创建了新的文件夹。git其实也正是一个文件系统。

harvey@harvey-Virtual-Machine:~$ rm -r -f demo4
harvey@harvey-Virtual-Machine:~$ git init demo4
Initialized empty Git repository in /home/harvey/demo4/.git/
harvey@harvey-Virtual-Machine:~$ cd demo4
harvey@harvey-Virtual-Machine:~/demo4$ echo "tttt">test
harvey@harvey-Virtual-Machine:~/demo4$ git add ./
harvey@harvey-Virtual-Machine:~/demo4$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test
#
harvey@harvey-Virtual-Machine:~/demo4$ rm test #清空工作空间,这时候工作空间是空的
harvey@harvey-Virtual-Machine:~/demo4$ git checkout master#checkou master失败
error: pathspec 'master' did not match any file(s) known to git.
harvey@harvey-Virtual-Machine:~/demo4$ git checkout . #checkout . 用工作空间的数据
harvey@harvey-Virtual-Machine:~/demo4$ ls#发现从缓存位置取出了缓存的数据
test

3.基于我们认为stage是和工作空间相同的工作目录,历史提交也是工作空间,也就是文件夹。那么我们就可以用diff命令来比较的时候,本质上也是不叫的文件夹。

harvey@harvey-Virtual-Machine:~$ rm -r -f demo4
harvey@harvey-Virtual-Machine:~$ git init demo4
Initialized empty Git repository in /home/harvey/demo4/.git/
harvey@harvey-Virtual-Machine:~$ cd demo4
harvey@harvey-Virtual-Machine:~/demo4$ mkdir a/a/
mkdir: 无法创建目录"a/a/": 没有那个文件或目录
harvey@harvey-Virtual-Machine:~/demo4$ mkdir a/a/ -p
harvey@harvey-Virtual-Machine:~/demo4$ echo "test1">a.txt #创建第一级别的文本文件
harvey@harvey-Virtual-Machine:~/demo4$ echo "test1">a/a/a.txt#创建文件夹下的文本文件
harvey@harvey-Virtual-Machine:~/demo4$ git add ./
harvey@harvey-Virtual-Machine:~/demo4$ git diff #提交在比较,因为此时两个文件夹是同步了的,所以diff没有输出内容,也就是两个文件夹没有差别
harvey@harvey-Virtual-Machine:~/demo4$ echo "ttttt">>a.txt #更改文件
harvey@harvey-Virtual-Machine:~/demo4$ echo "ttttt">>a/a/a.txt #更改文件
harvey@harvey-Virtual-Machine:~/demo4$ git diff #发现输出的内容是比较的两个文件的差异,所以是比较的是文件夹
diff --git a/a.txt b/a.txt
index a5bce3f..9f42087 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
test1
+ttttt
diff --git a/a/a/a.txt b/a/a/a.txt
index a5bce3f..9f42087 100644
--- a/a/a/a.txt
+++ b/a/a/a.txt
@@ -1 +1,2 @@
test1
+ttttt#发现git diff的文件比较顺序是 ”diff stage 工作空间“
harvey@harvey-Virtual-Machine:~/demo4$ git status -s #发现第一个A表示HEAD是空的,M表示stage和工作空间比较内容变化了,具体的变化内容只能通过diff来查看。也就是status显示的是摘要
AM a.txt
AM a/a/a.txt

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295