如何交换Git commit的两个父母的顺序? [英] How do I swap the order of two parents of a Git commit?

查看:755
本文介绍了如何交换Git commit的两个父母的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

合并提交是至少有两个父母的提交。这些父母是按特定顺序的。



如果我目前在分支 master 上,并且我在分支功能,我创建了一个新的提交,其第一个父提交来自 master ,并且第二个提交是从特性提交。通过运行 git log --first-parent ,这个顺序特别明显。

 <$ c $合并提交
| \
| *从`feature`
* |提交从`master`提交

假设我现在意识到订单是错误的:我打算通过运行 git checkout功能将分支 master 合并到功能中; git merge master 。我想交换合并提交的父母的顺序,但我不想再次解决所有合并冲突的麻烦。我怎样才能做到这一点?

  *合并提交
| \
* |从`feature`
|提交*从`master`提交


解决方案

真的很酷的命令我最近了解到,它会做你想要的:

  git commit-tree -p HEAD ^ 2 -p HEAD ^ 1 -mCommit messageHEAD ^ {tree}

这会创建一个新的提交基于什么是HEAD,但假设它的父母是HEAD ^ 2,HEAD ^ 1(注意这是颠倒的顺序)。



git-commit-tree将新版本打印为输出,因此您可以将它与git-reset-hard结合使用:

  git reset --hard $(git commit-tree -p HEAD ^ 2 -p HEAD ^ 1 -mNew commit messageHEAD ^ {tree}) 


A merge commit is a commit with at least two parents. These parents are in specific order.

If I'm currently on the branch master, and I merge in the branch feature, I create a new commit with its first parent being the commit from master, and the second commit being the commit from feature. This order is especially evident by running git log --first-parent.

*   The merge commit
|\
| * The commit from `feature`
* | The commit from `master`

Say I now realise that the order is the wrong way round: I intended to merge the branch master into feature by running git checkout feature; git merge master. I want to swap the order of the parents of a merge commit, but I do not want to go through the hassle of resolving all the merge conflicts again. How can I do this?

*   The merge commit
|\
* | The commit from `feature`
| * The commit from `master`

解决方案

Actualy, there's a really cool command I learned recently that will do exactly what you want:

git commit-tree -p HEAD^2 -p HEAD^1 -m "Commit message" "HEAD^{tree}" 

This will create a new commit based on what is currently HEAD, but pretend that it's parents were HEAD^2,HEAD^1 (note this is the reversed order).

git-commit-tree prints the new revision as output, so you might combine it with a git-reset-hard:

git reset --hard $(git commit-tree -p HEAD^2 -p HEAD^1 -m "New commit message" "HEAD^{tree}")

这篇关于如何交换Git commit的两个父母的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆