git学习笔记一

1.刚初始化的仓库是没有index文件的这个时候还没有执行add操作所以是不存在index文件的。并且objcts目录下面也只有info 和pack文件。

harvey@harvey-Virtual-Machine:~/demo1$ git init ../harvey #初始化git仓库
Initialized empty Git repository in /home/harvey/harvey/.git/
harvey@harvey-Virtual-Machine:~/demo1$ cd ../harvey/
harvey@harvey-Virtual-Machine:~/harvey$ ls -a #发现创建成功
.  ..  .git
harvey@harvey-Virtual-Machine:~/harvey$ tree .git/objects/ #新创建的objects对象只有这两个
.git/objects/
├── info
└── pack

2 directories, 0 files
harvey@harvey-Virtual-Machine:~/harvey$ tree  #.git目录下没有index索引文件
.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

9 directories, 12 files

2.第一次添加执行add操作以后,会首先创建index文件,和在object库里写入提交的文件的快照。这个新创建的index文件是一个二进制的文件,我们只能通过ls-files来查看他的内容,查看的其实也就是我们add进去的文件的列表,注意这个时候在stage里的内容只是列表,还不是tree,也就是还没有一个唯一的tree id可以让我们访问,这个时候,我们只能直接通过保存进去的object对象的来访问,其实也就是我们是不能直接把stage里的数据完全恢复到工作目录里的。不过,不用担心,因为我们的数据快照都被保存到git里了,所以我们可以通过commit命令来给stage里的数据创建一个唯一的tree id,这样我们就通过tree id来直接恢复文件到我们的工作目录了。git不允许单独创建tree,而是创建tree的时候必须创建commt对象。其实commit对象里,我们最关心的也只是tree,而commit对象里的parent id和user email 等信息是为了方便管理历史版本的。所有我们以后的所有的对分支的操作都是基于这样一个事实,那就是只有tree是直接代表的历史的具体数据,别的都是为了管理tree的一些附件参数,虽然附件的参数也很重要。在工作区的文件实际上已经满足工作目录的tree结构了,只是没有一个id唯一的指向而已。只是生成新的工作树的前奏了。

harvey@harvey-Virtual-Machine:~/harvey$ touch test1.txt #直接在工作区创建一个空的文件
harvey@harvey-Virtual-Machine:~/harvey$ mkdir -p a/b/c/d #在工作区创建目录
harvey@harvey-Virtual-Machine:~/harvey$ touch a/b/c/d/test2.txt #在目录下创建文件
harvey@harvey-Virtual-Machine:~/harvey$ git add test1.txt a/b/c/d/test2.txt #添加到stage里开始观察
harvey@harvey-Virtual-Machine:~/harvey$ git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   a/b/c/d/test2.txt    #提示stage里有两个新的文件
#    new file:   test1.txt
#
harvey@harvey-Virtual-Machine:~/harvey$ git ls-files --stage #用ls-files查看这两个新家的文件的object对象是什么,我们可以根据这两个长的id来查看文件的内容
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0    a/b/c/d/test2.txt
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0    test1.txt
harvey@harvey-Virtual-Machine:~/harvey$ tree .git/objects/ #查看.git发现多了一个文件--至于为什么不是两个文件而是一个,现在还没研究到
.git/objects/
├── e6
│   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
├── info
└── pack

3 directories, 1 file
harvey@harvey-Virtual-Machine:~/harvey$ tree .git/ #查看发现.git出现了index,这是因为add的效果,并且我们ls-files就是读取的这个文件
.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── objects
│   ├── e6
│   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

10 directories, 14 files
harvey@harvey-Virtual-Machine:~/harvey$ git cat-file -p e69de29

3.stage里的数据作为文件列表存在在数据库中了,我们可以分多次向stage里添加数据,直到我们绝的我们认为我们的修改可以提交位置,每一次的add都是王stage这个列表里添加数据。实际上我们不仅可以从工作目录里往列表添加数据,即所谓的添加新数据。实际上我们也可以往stage里添加就的数据就是历史版本的更新信息。

  • 创建3次提交,第一次为创建a/a/a/a.txt和a.txt 内容都是a
  • 第二次创建b/b/b/b.txt 和b.txt 内容是b
  • 第三次创建c/c/c/c.txt和c.txt 内容是c
    harvey@harvey-Virtual-Machine:~/test$ git log --pretty=oneline #查看git log发现三次提交都成功了
    81c4736e7e2014e6f210fc84a2d6c20b2b55714b c
    f48af8776eef36ada3083ffc2414f65eb8e71bd5 b
    64ed14fa53169a5d6c200afe66078e90bb571a61 a
    harvey@harvey-Virtual-Machine:~/test$ git status #查看到现在的状态是clean就是都是干净的
    # On branch master
    nothing to commit (working directory clean)
    harvey@harvey-Virtual-Machine:~/test$ git ls-files --stage #查看index文件的信息发现所有的文件都是在这个列表里的
    100644 78981922613b2afb6025042ff6bd878ac1994e85 0    a.txt
    100644 78981922613b2afb6025042ff6bd878ac1994e85 0    a/a/a/a.txt
    100644 61780798228d17af2d34fce4cfbdf35556832472 0    b.txt
    100644 61780798228d17af2d34fce4cfbdf35556832472 0    b/b/b/b.txt
    100644 f2ad6c76f0115a6ba5b00456a849810e7ec0af20 0    c.txt
    100644 f2ad6c76f0115a6ba5b00456a849810e7ec0af20 0    c/c/c/c.txt
    harvey@harvey-Virtual-Machine:~/test$ tree .git/objects/ #查看发现新的,git里面的多了很多的object兑现,不错和stage里的id名字并不相同 看到stage里的内容,我们就知道为什么 直接执行git diff我们就可以比较出刚修改的文件的内容了,因为最后一次提交的数据和之前几次提交的所有数据都是在stage里的
    .git/objects/
    ├── 08
    │   └── 585692ce06452da6f82ae66b90d98b55536fca
    ├── 2a
    │   └── c33aa68b66941f9772ca253efaadc2c596d2a9
    ├── 2e
    │   └── ef31f20c83a373a0e644a1cfad649185e06e8c
    ├── 32
    │   └── ca469287bd11315303cec54b25f1aa1c6c956e
    ├── 3f
    │   └── c3a14b8394c7c995c0cd7bd709ca49ef655ffa
    ├── 61
    │   └── 780798228d17af2d34fce4cfbdf35556832472
    ├── 64
    │   ├── 954db95fa9bfd346c935e3d18e507b0de33b20
    │   └── ed14fa53169a5d6c200afe66078e90bb571a61
    ├── 6d
    │   └── d2e047b2d563908790f063b8927dad78435b67
    ├── 78
    │   └── 981922613b2afb6025042ff6bd878ac1994e85
    ├── 81
    │   └── c4736e7e2014e6f210fc84a2d6c20b2b55714b
    ├── a3
    │   └── 3a85e1ebb2e91deaeda9580a8512d90ce997b7
    ├── c4
    │   └── 4a01904d9d7fce1cdf3f7045a2c2aeef8a8318
    ├── cf
    │   └── 67e9ef3a0fc6d858423fc177f2fbbe985a6f17
    ├── f2
    │   └── ad6c76f0115a6ba5b00456a849810e7ec0af20
    ├── f4
    │   └── 8af8776eef36ada3083ffc2414f65eb8e71bd5
    ├── f8
    │   └── f7aefc2900a3d737cea9eee45729fd55761e1a
    ├── fa
    │   └── 57da9ba8bcf485b98f090a6f84b92c6de5489a
    ├── info
    └── pack