删除提交的父级,从而使其成为初始提交



注意:我知道这是在重写历史,会改变所有的哈希值,把其他人搞砸。


我想提交并删除其父级。具体来说,提交现在应该看起来像初始提交。这意味着它的差异会改变;看起来好像所有文件都是由该提交在历史记录的那个时刻创建的。

如何做到这一点?(当然,我可以编辑提交对象,但这样提交就不会相互指向。另外,我想在一个新的分支上执行此操作(这意味着现在有两个历史记录:原始历史记录,另一个提交是初始提交。

git rebase -i HEAD~

然后在上述终端cmd之后弹出的编辑器中,将子提交行的开头从pick更改为挤压。或分别是"p"和"s"。写入文件并退出上述命令中出现的编辑器

编辑:假设两个提交是最新的。如果没有,那么你可以签出孩子的子提交哈希并使用 git rebase -i HEAD~2 代替

Edit2:或者你可以只 git rebase -i parentcommithash,列表的顶部应该是父级

git checkout -b newbranch
git rev-parse @ >.git/info/grafts   # any equivalent for @ will work e.g. HEAD or newbranch
git filter-branch
rm .git/info/grafts

(编辑:添加了rm。没有这个,这个回购仍将反映原始提交的嫁接祖先)

假设您希望新的初始提交123456

git checkout -b new_master
git filter-branch --commit-filter '
  if git merge-base --is-ancestor 123456 $GIT_COMMIT ;
  then
   git commit-tree "$@";
  else
   skip_commit "$@";
  fi' HEAD

待办事项(适用于任何人):在说明中编辑

似乎您想要的是压制自初始提交以来的所有提交,直到(包括)您想要成为新的初始提交的提交。

您可以使用 git rebase --interactive --root .这将为您提供一个包含所有提交的待办事项文件。然后继续编辑从秒到挤压的所有提交,它看起来像这样:

pick <hash> initial commit
squash <hash> want change but not commit
squash <hash> want change but not commit
squash <hash> want change but not commit
squash <hash> want change but not commit
squash <hash> this will be new init
pick <hash> this will be the second commit
pick <hash> and so on...

系统将提示您为生成的 squash 编写提交消息。

最新更新