深入理解Git (四) -微命令下篇


6 git commit-tree 

这个命令可以用来生成commit对象,commit对象是tree对象的扩展,除了指引tree对象外,还有一些额外的信息,比如此次commit的info,谁commit的,什么时间等。

echo "initial commit" | git commit-tree 4e1ba2916b4b903d2e5a2209cdcca7129a891c2f  

fb1c75a143de21f30004dab829b39f99d225d292

同理,我们看一下:

git cat-file -t fb1c75a143de21f30004dab829b39f99d225d292

commit


git cat-file -p fb1c75a143de21f30004dab829b39f99d225d292

tree 4e1ba2916b4b903d2e5a2209cdcca7129a891c2f
author hongchangfirst <[email protected]> 1430193593 +0000
committer hongchangfirst <[email protected]> 1430193593 +0000


initial commit

我们看到commit对象自动包含了tree对象的索引,和author的一些信息,另外加上了我们自己写的commit信息。


现在我们就可以git show出来这个commit的信息。

git show commit fb1c75a143de21f30004dab829b39f99d225d292
那么git log呢?

git log

fatal: bad default revision 'HEAD'

这是为什么呢?因为Git会去找.git/HEAD文件,看到里边的内容是ref: refs/heads/master,然后就去找.git/refs/heads/master,非常悲催的发现此文件不存在,那么怎么办呢?


7 ref对象

解决上边的问题很简单,我们只需要更新/refs/heads/master即可。

echo 'fb1c75a143de21f30004dab829b39f99d225d292' > .git/refs/heads/master

现在master就是fb1c75a143de21f30004dab829b39f99d225d292此commit的别名。

我们可以直接

git show master

git log


所以我们清晰的看到了一个链条:

HEAD -> commit对象 -> tree对象 -> blob对象


上边这些微命令如果我们用起来估计要累死了,所以git又聚合了其中的常用命令,比如:

1 git add就是git hash-object和git update-index的聚合,也就是说git add创建blob对象,并且自动将该blob对象添加到cache中。

2 git commit就是write-tree和commit-tree的聚合操作,也就是说git commit会将cache中的对象生成tree对象,并且根据此tree对象生成commit对象。


8 Branch

分支其实是引用,如果想知道分支引用哪个commit对象的话,可以用rev-parse

git rev-parse master

fb1c75a143de21f30004dab829b39f99d225d292


你可以git log一下,看到这个就是位于最顶层的commit对象指针。


原文:http://blog.csdn.net/hongchangfirst/article/details/47445007

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst



你可能感兴趣的:(深入理解Git (四) -微命令下篇)