我正在阅读有关 git 对象的信息:blob、tree、commit、tag。为了更好地理解 git 的工作原理,我尝试了一些低级命令,如
write-tree
和 commit-tree
。
mkdir test; cd test
--> git init
git add file
。我可以看到在 .git/objects
git write-tree
打印当前树IDgit commit-tree treeID -m "commit a tree"
提交这棵树。执行此操作后,生成了一个提交对象,我可以看到它确实包含作者、日期等。但是,我无法使用 git log
检查我的提交,错误是:fatal: bad default revision 'HEAD'
。 完成上述操作后,当我运行
git status
时,我看到文件仍在索引中等待提交。 commit-tree
有什么用,commit-tree
和`commit'有什么区别?
git-commit - 记录对存储库的更改
在新提交中存储索引的当前内容以及来自用户的描述更改的日志消息。
git commit "records changes to the repository"
git-commit
的图表表示显示在 here 在 SO
git-commit-tree - 创建一个新的提交对象
根据提供的树对象创建一个新的提交对象,并在
stdout
. 上发出新的提交对象 id
这通常不是最终用户想要直接运行的。创建一个 基于提供的树对象的新提交对象并发出新的 在标准输出上提交对象 ID。日志消息是从标准中读取的 输入,除非给出
或-m
选项。-F
git-commit(1) 是你想要使用几乎所有的高级命令 的时间。 git-commit-tree(1) 是一个较低级别的命令,它不是 日常使用命令。
git-commit(1) 在您以交互方式提交新更改时使用。但是我 我想我不必详细说明。
git-commit-tree(1) 在以下情况下很有用:
说我想有效地“压缩”回购协议中的所有提交 一次提交。但是“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
man gitrevisions
语法,请参阅^{tree}
git checkout --orphan new-start && git commit -m Init
。所以这不像
你需要这个命令来做它。