git commit和git commit-tree有什么区别

问题描述 投票:0回答:2

我正在阅读有关 git 对象的信息:blob、tree、commit、tag。为了更好地理解 git 的工作原理,我尝试了一些低级命令,如

write-tree
commit-tree

  1. mkdir test; cd test
    -->
    git init
  2. 我创建一个文件和
    git add file
    。我可以看到在
    .git/objects
  3. 中生成了一个 blob 和树对象
  4. git write-tree
    打印当前树ID
  5. git commit-tree treeID -m "commit a tree"
    提交这棵树。执行此操作后,生成了一个提交对象,我可以看到它确实包含作者、日期等。但是,我无法使用
    git log
    检查我的提交,错误是:
    fatal: bad default revision 'HEAD'

完成上述操作后,当我运行

git status
时,我看到文件仍在索引中等待提交。
commit-tree
有什么用,
commit-tree
和`commit'有什么区别?

git commit
2个回答
7
投票

git-commit - 记录对存储库的更改

在新提交中存储索引的当前内容以及来自用户的描述更改的日志消息。

git commit "records changes to the repository"

git-commit
的图表表示显示在 here 在 SO

git-commit-tree - 创建一个新的提交对象

根据提供的树对象创建一个新的提交对象,并在

stdout
.

上发出新的提交对象 id

这通常不是最终用户想要直接运行的。创建一个 基于提供的树对象的新提交对象并发出新的 在标准输出上提交对象 ID。日志消息是从标准中读取的 输入,除非给出

-m
-F
选项。


0
投票

git-commit(1) 是你想要使用几乎所有的高级命令 的时间。 git-commit-tree(1) 是一个较低级别的命令,它不是 日常使用命令。

git-commit(1) 在您以交互方式提交新更改时使用。但是我 我想我不必详细说明。

git-commit-tree(1) 在以下情况下很有用:

  1. 想要明确选择提交的父母
  2. 你已经有了一个 tree 提交

说我想有效地“压缩”回购协议中的所有提交 一次提交。但是“squash”是一个 rebase 操作,它是 过度,因为它结合了变化,我不需要结合任何 变化,因为我已经知道我想要什么快照——当前 一。所以:[1] [2]

$ git commit-tree -m Init HEAD^{tree}
1a875b50b508c70e44a4e6300fd80f580aed3d99

(这变成了一个没有父母的承诺,因为我没有指定任何父母

-p
.)

那是提交的 SHA1。我最好把它放在 分支以便垃圾收集器不会在几个月后收集它 现在。

$ git branch new-start 1a875b50b508c70e44a4e6300fd80f580aed3d99

您可以做的另一件事是创建您想要使用的任何历史记录 现有提交及其树。比如说我想做一个 Git 存储库的历史,它只包含版本

v1.0.0
v2.0.0
,和
v2.40.0
并致电分行
small-git

git clone https://github.com/git/git/ git-repo
cd git-repo
first=$(git commit-tree -m v1 v1.0.0^{tree})
second=$(git commit-tree -p $first -m v2 v2.0.0^{tree})
third=$(git commit-tree -p $second -m v2.40.0 v2.40.0^{tree})
git branch small-git $third

你可以用这个来验证(所有差异都应该是空的):

$ # While on `small-git`
$ git diff v2.40.0
$ git diff @^ v2.0.0
$ git diff @^^ v1.0.0

注意事项

  1. 有关
    man gitrevisions
    语法,请参阅
    ^{tree}
  2. 这可以通过其他方式来完成,例如
    git checkout --orphan new-start && git commit -m Init
    。所以这不像 你需要这个命令来做它。
© www.soinside.com 2019 - 2024. All rights reserved.