赞
踩
参考源站了解更多: Git讲之git commit
git commit
命令将Staging阶段的代码提交至History
状态.
我们以上面的文件为例,进行commit提交:
$ git commit -m "add README.txt" [master (root-commit) be57548] add README.txt 1 file changed, 1 insertion(+) create mode 100644 README.txt $ git log commit be57548a25efd89b6e686b622188ad2135cb246b (HEAD -> master) Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 16:12:39 2020 +0800 add README.txt $ git cat-file -t be57548a25efd89b6e686b622188ad2135cb246b #commit操作后,得到一个commit类型对象 commit $ git cat-file -p be57548a25efd89b6e686b622188ad2135cb246b tree 3ff7ec4f18fa77a0277bd1397b3248c49d9c5ba4 #commit类型的对象包含一个tree类型的对象 author jiangjian <jiangjian199204@gmail.com> 1608106359 +0800 committer jiangjian <jiangjian199204@gmail.com> 1608106359 +0800 add README.txt $ git cat-file -t 3ff7ec4f18fa77a0277bd1397b3248c49d9c5ba4 tree $ git cat-file -p 3ff7ec4f18fa77a0277bd1397b3248c49d9c5ba4 #tree类型的对象里面记录着tree对象的子节点 100644 blob 5da849b5c6f00b5292b1a823b29ceb303e98585c README.txt
上面的操作过程中,我们得到一个commit
类型的对象,通过该对象内部包含的tree
类型对象,我们可以将当前commit指向的tree对象的所有子节点遍历,就可以得到当前commit状态的整个文件状态,我们可以将上面的3个类型对象进行关联起来.
我们再继续添加一个commit来了解一下commit之间如何关系的, 一顿操作如下:
$ echo "print 'hello world'" > 1.py #新增一个1.py的文件 $ git add 1.py #将1.py文件加到Staging 区域 warning: LF will be replaced by CRLF in 1.py. The file will have its original line endings in your working directory $ git commit -m "add 1.py file" # 将1.py文件从Staging 区域转移到History区域 [master 3ca8f94] add 1.py file 1 file changed, 1 insertion(+) create mode 100644 1.py $ git log # 查看git commit记录,可以看到有两个commit log记录,默认排序按时间倒序 commit 3ca8f948d5d93569fd87806cf71bddf93d1bb00c (HEAD -> master) Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 16:53:19 2020 +0800 add 1.py file commit be57548a25efd89b6e686b622188ad2135cb246b Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 16:12:39 2020 +0800 add README.txt $ git cat-file -p 3ca8f948d5d93569fd87806cf71bddf93d1bb00c # 我们查看最近提交的commit对象内容 tree af324ffc63f85247ee61e8538fced865915fddce # commit parent be57548a25efd89b6e686b622188ad2135cb246b # commit对象存在父子关系,当前的commit父节点是上一次提交commit的id author jiangjian <jiangjian199204@gmail.com> 1608108799 +0800 committer jiangjian <jiangjian199204@gmail.com> 1608108799 +0800 add 1.py file $ git cat-file -p af324ffc63f85247ee61e8538fced865915fddce # 我们查看最近提交commit中tree对象的内容,可以了解到里面有两个blob,因为我们连续添加了两个文件,所以通过遍历该tree的子节点和内容可以将commit提交时的整个工程状态进行还原 100644 blob a968078c845da96a55c4ba1b21c38cdef55ddf2a 1.py 100644 blob 5da849b5c6f00b5292b1a823b29ceb303e98585c README.txt
我们通过如下的图可以了解到整个的关系:
接下来,我们看一下添加目录会涉及到什么新事物:
$ mkdir log # 创建log目录 $ echo "1" > log/1.log $ git add . warning: LF will be replaced by CRLF in log/1.log. The file will have its original line endings in your working directory $ git commit -m "add log file" [master 5ede504] add log file 1 file changed, 1 insertion(+) create mode 100644 log/1.log $ git log commit 5ede5043e4d3037c691f3344a2dfda4ec82b869a (HEAD -> master) Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 17:31:44 2020 +0800 add log file commit 3ca8f948d5d93569fd87806cf71bddf93d1bb00c Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 16:53:19 2020 +0800 add 1.py file commit be57548a25efd89b6e686b622188ad2135cb246b Author: jiangjian <jiangjian199204@gmail.com> Date: Wed Dec 16 16:12:39 2020 +0800 add README.txt $ git cat-file -p 5ede5043e4d3037c691f3344a2dfda4ec82b869a tree 5340eab424a090849b51164cbc8dc5c5396fe82b parent 3ca8f948d5d93569fd87806cf71bddf93d1bb00c author jiangjian <jiangjian199204@gmail.com> 1608111104 +0800 committer jiangjian <jiangjian199204@gmail.com> 1608111104 +0800 add log file $ git cat-file -p 5340eab424a090849b51164cbc8dc5c5396fe82b 100644 blob a968078c845da96a55c4ba1b21c38cdef55ddf2a 1.py 100644 blob 5da849b5c6f00b5292b1a823b29ceb303e98585c README.txt 040000 tree be2f1df0ca9f269f282a0baa9f247f8c03438a02 log $ git cat-file -p be2f1df0ca9f269f282a0baa9f247f8c03438a02 100644 blob d00491fd7e5bb6fa28c517a0bb32b8b506539d4d 1.log $ git cat-file -p d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
通过上面的图示可以了解到新建的目录都会新增一个tree类型节点用来代表该目录
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。