Git commit-tree 与 Git commit的区别

'git commit-tree'指令基于一个tree的hash id创建了一个commit对象。

'git commit'则是将暂存区的内容放到仓库。暂存区的通常通常是一个commit对象。

所以,我们可以看到,要将这个commit-tree放入仓库中我们仍需要额外的操作。让我们再来模拟下这个过程。

[wlin@wlin local_version]$ git init
Initialized empty Git repository in /home/wlin/local_version/.git/
[wlin@wlin local_version]$ echo "version 1" > test.txt
[wlin@wlin local_version]$ git hash-object -w test.txt
83baae61804e65cc73a7201a7252750c76066a30
[wlin@wlin local_version]$ git update-index --add --cacheinfo 100644 83baae61804e65cc73a7201a7252750c76066a30 test.txt
[wlin@wlin local_version]$ git write-tree
d8329fc1cc938780ffdd9f94e0d364e0ea74f579
[wlin@wlin local_version]$ echo "first commit" | git commit-tree d8329fc1cc938780ffdd9f94e0d364e0ea74f579
c51498ffa6d93bbe5661249a2e58d43ce0e45924
# 并没有commit生成
[wlin@wlin local_version]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   test.txt
#
# 也没有log生成,显示的错误原因是HEAD没有指定有效的版本
[wlin@wlin local_version]$ git log
fatal: bad default revision 'HEAD'
# 更新master(如果你在其他分支上,则需要指定为其他分支)分支的head的值,指向我们的commit对象
[wlin@wlin local_version]$ git update-ref refs/heads/master b79cf1e3cdb481442f5e11d8bef81466b0f6305f
# 显示我们的commit
[wlin@wlin local_version]$ git log
commit c51498ffa6d93bbe5661249a2e58d43ce0e45924
Author: Cara Wang <[email protected]>
Date:   Wed Jan 23 16:53:05 2019 +0100

    first commit

猜你喜欢

转载自blog.csdn.net/solinger/article/details/86619206