【问题标题】:Need to split old pushed commit on the branch to many需要将分支上的旧推送提交拆分为多个
【发布时间】:2023-04-02 07:45:01
【问题描述】:

分支上有 30 个推送的提交。有必要将分支上的第 2 次提交拆分为 20 多个提交,而不会损坏以下提交(在第 1 次和接下来的 28 次提交之间插入拆分提交)。分支上的第二次提交有一堆可以放在 20 多个提交中的更改。

是否可以在不破坏后续提交的情况下进行拆分?

【问题讨论】:

  • 你是什么意思“破坏”?当然,您必须重写它们。
  • 损坏 = 不丢失数据。需要重写所有 28 个下一次提交?哦
  • 是的,当然,因为现在他们会有新父母了。
  • 你能写指令怎么做+-用git命令快速但不丢失数据吗?)

标签: git git-commit git-rebase git-remote git-reset


【解决方案1】:

好的...这需要您做一些工作。简短的回答是yes,这是可能的。长答案:这需要一些工作。

git branch second second-commit-id
# we have placed a commit on the original second revision
git checkout -b temp second-commit~
# we checkout a new branch called temp placed on the parent of second... this is where the trick starts

每次迭代都需要您遵循这些步骤(应该有 20 个,对吧?)

git checkout --detach second # we place ourselves on the target _content_ revision... detached HEAD
git reset --soft temp # set branch pointer on the previous iteration revision
# now you start checking the files and leave the content you want it to be for _this_ iteration...
# compare contents with both previous iteration (temp) and target revision (second)
# add the files when you are ready
git commit -m "A new iteration... write the proper message"
# now we need to set temp where we are
git branch -f temp # temp is now where we are

您将在多次迭代中重复此操作,直到达到 temp 像秒的点

然后你可以继续:

# replicate all changes from second up to the tip of the real branch
git cherry-pick second..master # or whatever branch you are rewriting
# if you like the result
git branch -f master # place master or whatever branch over here
git checkout master

现在你已经完成了你的事情。

【讨论】:

  • 非常感谢。希望对我有帮助。
【解决方案2】:

分支上有 30 个推送的提交。必须将分支上的第二次提交拆分为 20+ 次提交,而不会损坏以下提交

git checkout -b newbranch thatcommit
git reset @^

现在您在一个新分支上,您的工作树包含thatcommit 快照,并且您的索引和父级设置为其父级。连续添加并提交每个替换提交的更改:

git add --patch   # or --interactive
git commit

或者,如果幸运的话,这些更改是独立的,包含在单独的文件中,您可以只提交单个文件(有关在提交时指定一个或多个路径时发生的情况,请参阅文档)。

现在您已经有了 thatcommit 的替换系列。在本地将每个提交设置为 thatcommit 父级,以将您的替换提示作为父级:

thatcommit=`git rev-parse thatcommit`
newbranch=`git rev-parse newbranch`
git rev-list --ancestry-path --all --parents ^thatcommit \
| sed -n s/$thatcommit/$newbranch/p > .git/info/grafts

并使用git filter-branch 烘烤重新布线的祖先:

git filter-branch -- --all

如果您不喜欢结果,请退出:

git fetch . +refs/original/*:*

并清理(无论哪种方式):

git filter-branch -f --setup exit
rm -f .git/info/grafts

【讨论】:

  • 非常感谢。希望对我有帮助。
  • 最后两个命令中的任何一个都不需要立即使用,只是删除了以后可能会烦人的东西。
猜你喜欢
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 2019-09-29
  • 2012-12-03
  • 2012-10-11
  • 2012-10-22
  • 2020-11-30
  • 2015-07-24
相关资源
最近更新 更多