git - 如何添加新的根提交?
问题描述
我需要模拟一个异常的提交历史来编写一个测试用例,我想要一个提交历史,比如:
sha4
sha3 // i want this to be a root commit
sha2
sha1 // this is the first commit of the repo, so it's a root commit
我希望git rev-list
展示:
$ git rev-list --max-parents=0 HEAD
sha3
sha1
我的最终目标是让git rev-list --max-parents=0 HEAD
返回多行。
解决方案
sha3
无法转换为根提交。提交是不可变的。但是可以创建新的根提交。
通过孤立分支,假设当前分支是master
,
git checkout --orphan temp sha3
git commit -m 'init temp'
git checkout master
git merge temp --allow-unrelated-histories
通过空树对象4b825dc642cb6eb9a060e54bf8d69288fbee4904
,
git merge --allow-unrelated-histories $(git commit-tree -m'new root commit' 4b825dc642cb6eb9a060e54bf8d69288fbee4904)
无论哪种方式,git rev-list --max-parents=0 HEAD
现在都会打印 2 个根提交。
几乎不可能记住空树哈希。您可以使用该命令git hash-object -w -t tree --stdin < /dev/null
创建树并获取其哈希值。
事实上,为了创建根提交,我们不必使用空树。我们可以使用任何现有的树。
git merge --allow-unrelated-histories $(git commit-tree -m'new root commit' sha3^{tree})
这里sha3
可以替换为任何其他现有的提交。空树的优点是不会引起合并冲突。
推荐阅读
- c# - RestSharp Post Json 对象
- javascript - 使用 JavaScript 下载图像
- c# - SWIG C# 使用 Director =“1”传递二进制数据
- nginx - nginx docker compose 未启动并运行
- javascript - 成功验证后将表单数据发送到 .php 文件的 Ajax 代码
- python - 没有root权限时如何使用pgadmin python
- r - 如何在 Shiny 中对多个表进行排序
- python - 有没有办法在没有 pyopencl 的 amd gpu 上运行 python 脚本
- node.js - 错误:在 MERN 堆栈应用程序中,没有从渲染返回任何内容...
- c# - 在 c# 中连接两个项目(在 Navisworks 中创建插件)