如何为一个提交指定一个或多个父代

如何为一个提交指定一个或多个父代

问题描述:

我已经完成了合并,并且创建了新的合并提交。然后,我将软重置分支一提交以保留具有分辨率的所有文件,恢复一个分辨率,现在我想使用所有这些文件重新创建合并提交。我有两个来自原始合并提交的父项哈希,并且希望在创建新提交时模拟合并提交时以某种方式指定这些父项。是否有可能?

I've done a merge and the new merge commit has been created. Then I soft reset the branch one commit back to preserve all files with resolutions, reverted one resolution and now I want to create merge commit again using all those files. I have two parent's hashes from the original merge commit and want to somehow specify these parents when creating a new commit to emulate merge commit. Is it possible?

除了 Mark Adelsberger的答案,有一个直接的方法可以用两个管道命令来做到这一点:

Besides Mark Adelsberger's answer, there's a direct way to do this with two plumbing commands:

tree=$(git write-tree) # write updated index to new tree
commit=$(git commit-tree -p $firstparent -p $secondparent -F /tmp/msgfile)

(其中 / tmp / msgfile 包含提交信息;请注意,您可以使用 -m 来指定消息文本,或 -F - 从stdin读取消息,在这种情况下,您可以 git log --pretty =%B $ hash 来提取原始提交信息,并将其传递给 git commit-tree )。

(where /tmp/msgfile contains the commit message; note that you can use -m to specify message text, or -F - to read the message from stdin, in which case you could git log --pretty=%B $hash to extract the original commit message and pipe that to git commit-tree).

完成提交后,您可以 git merge --ff-only git reset --hard $ c>给它,例如:

Once you have the commit you can git merge --ff-only or git reset --hard to it, e.g.:

git merge --ff-only $commit

不过,我只是想知道y ou表示:

I wonder, though, just what you mean by:


还原了一个分辨率

reverted one resolution



and in all cases the result is what is generally called an "evil merge": a merge whose result is not based on its inputs. Whether such a merge is truly evil, or even just a bad idea, is a matter of opinion, but it's certainly hard to reproduce later, and seems to call for a good commit message.