如何将其他父母添加到旧的git commits中? [英] How to add additional parents to old git commits?

查看:92
本文介绍了如何将其他父母添加到旧的git commits中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个分支的项目:master和gh-pages.它们本质上是两个不同的项目,其中gh-pages项目取决于主项目(反之亦然).可以将其视为"master包含源代码,gh-pages包含从这些源文件构建的二进制文件".定期,我对master中累积的更改进行处理,并使用提交消息与主提交xxxxxxxx保持一致"对gh-pages分支进行新提交.

I have a project containing two branches: master and gh-pages. They are essentially two different projects, where the gh-pages project depends on the master project (and not vice versa). Think of it as "master contains the source code, gh-pages contains the binary built from those source files". Periodically, I take the changes that have accumulated in master and make a new commit to the gh-pages branch with the commit message "Bring into line with master commit xxxxxxxx."

一段时间后,我意识到,如果gh页提交与主提交xxxxxxxx保持一致"实际上在git存储库中以xxxxxxxx作为其父级,那将是很好的.像这样(糟糕的MSPaint艺术):

After a while of doing this, I realized that it would be nice if the gh-pages commit "Bring into line with master commit xxxxxxxx" actually had xxxxxxxx as its parent in the git repository. Like this (bad MSPaint art):

是否有一种方法可以使存储库看起来像上面的第二个图像?我知道如何按照这种模式进行新的提交:我可以执行"git merge -s ours master"(设置否则为空提交的父项),然后执行"git commit --amend adv550.z8"(其中adv550.z8)是实际更改的二进制文件).但是git是否可以轻松地使时光倒流并将新的父母添加到旧的提交中?

Is there a way to make the repository look like the second image above? I know how to make new commits follow this pattern: I can do "git merge -s ours master" (which sets the parents of an otherwise empty commit) followed by "git commit --amend adv550.z8" (where adv550.z8 is the binary file that's actually changing). But does git make it easy to go back in time and add new parents to old commits?

一旦我的本地仓库看起来正确,我非常愿意"git push -f"并销毁我的Github存储库的当前历史记录.问题是, 我可以让我的本地存储库看起来正确吗?

I am perfectly willing to "git push -f" and blow away the current history of my Github repository, once I get my local repo looking right. The question is, can I get my local repo looking right?

以后要编辑的年份:我最终放弃了使gh-pages的git历史看起来像这样的尝试;我认为对于零增益而言,这太繁琐了.我的新做法是积极压缩对gh-pages的提交,因为在我的情况下,保存那些提交消息确实无关紧要. (这只是一长串与主提交保持一致...",在历史上都不是有趣的.)但是,如果我需要再次执行此操作,我会听听说的答案

EDITED YEARS LATER TO ADD: I eventually abandoned my attempts to make the git history of gh-pages look like that; I decided that it was too much work for zero gain. My new practice is to aggressively squash commits to gh-pages, because saving those commit messages really doesn't matter in my case. (It's just a long line of "Bring into line with master commit..."s, none of which are historically interesting.) However, if I needed to do this again, I'd listen to the answers that said

git merge $intended_parent_1 $intended_parent_2
git checkout $original_commit -- .
git commit --amend -a -C $original_commit

推荐答案

您无法及时返回并更改现有提交.即使您执行git commit --amend之类的操作,您实际上也没有更改提交.您将在树中的同一位置创建一个新文件,其中包含原始文件中的所有内容(以及您所做的更改).您会注意到,提交哈希在--amend之后发生了变化,并且原始内容仍然存在于您的存储库中–您可以使用git reflog重新获得它.

You can't go back in time and change existing commits. Even when you do something like git commit --amend, you're not actually changing the commit; you're creating a new one at the same place in the tree with all the content from the original (plus your changes). You'll notice that the commit hash changes after the --amend, and the original is still present in your repo--you can get back to it with git reflog.

另一方面,您可以可以回到过去并创建一个替代宇宙.本质上,您将返回到您的gh-pages分支点,并将整个内容(例如,git cherry-pick或类似内容)重新创建为并行分支.哈希会改变,因为提交是不同的对象.

On the other hand, you can go back in time and create an alternate universe. Essentially, you would go back to your gh-pages branch point and recreate the whole thing (with e.g. git cherry-pick or something) as a parallel branch. The hashes would change, because the commits are different objects.

(我很好奇,为什么要将回购设置为单独的分支,而不是将同一目录中的单独目录设置.似乎代码->构建过程会很繁琐.)

(I'm curious why you have your repo set up as separate branches, instead of separate directories in the same branch. It seems like the code -> build process would get tedious.)

这篇关于如何将其他父母添加到旧的git commits中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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