git - 如何添加新的根提交?

标签 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可以被任何其他现有提交替换。空树的优点是不会引起 merge 冲突。

关于git - 如何添加新的根提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62297038/

相关文章:

git - 从 Git 存储库中 fork 一个文件而不克隆存储库

windows - git 克隆卡在 "checking connectivity"

git - github SSH 是如何工作的?

git - Git 提交范围中的双点 ".."和三点 "..."有什么区别?

.net - Windows Phone 8 应用程序在 git 控制下无法在模拟器上运行

git - 我正在尝试推送到 Heroku,但它说我的指纹不正确

git - 如何从 Netbeans 重命名 Git 修订文件

git - 有没有办法缩短 git 命令中的分支名称?

git - 删除请求存储库时了解 GitHub : What happens, 上的 pull 请求?

Git:将包含先前精心挑选的提交的分支与 master 同步