您可以使用自定义合并驱动程序“keepTheirs”将您的上游分支合并到您的dev 分支:
请参阅““git merge -s theirs” needed — but I know it doesn't exist”。
在您的情况下,只需要一个 .gitattributes 和一个 keepTheirs 脚本,例如:
mv -f $3 $2
exit 0
git merge --strategy=theirs模拟#1
显示为合并,上游为第一个父级。
Jefromi 提到(在 cmets 中)merge -s ours,通过合并您在上游(或从上游开始的临时分支)上的工作,然后将您的分支快速转发到该合并的结果:
git checkout -b tmp origin/upstream
git merge -s ours downstream # ignoring all changes from downstream
git checkout downstream
git merge tmp # fast-forward to tmp HEAD
git branch -D tmp # deleting tmp
这样做的好处是把上游的祖先记录为第一个父母,这样合并的意思是“吸收这个过时的主题分支”而不是“销毁这个主题分支并用上游代替”。
(2011 年编辑):
此工作流程已在此blog post by the OP 中报告:
为什么我又想要这个?
只要我的 repo 与公共版本无关,这一切都很好,但现在我希望能够与其他团队成员和外部贡献者在 WIP 上进行协作,我想确保我的公共分支对于其他人来说是可靠的分支和拉取,即不再对我推送到远程备份的内容进行变基和重置,因为它现在在 GitHub 和公共上。
所以这让我知道我应该如何进行。
99% 的时间我的副本会进入上游 master,所以我想大部分时间都在我的 master 工作并推送到上游。
但是每隔一段时间,我在wip 中的内容会因进入上游的内容而失效,我将放弃我的wip 的某些部分。
那时我想把我的主人带回来与上游同步,但不会破坏我公开推送的 master 上的任何提交点。 IE。我想要与上游合并,最终得到使我的副本与上游相同的变更集。
这就是git merge --strategy=theirs 应该做的。
git merge --strategy=theirs模拟#2
显示为合并,我们的作为第一个父级。
(由jcwenger提议)
git checkout -b tmp upstream
git merge -s ours thebranch # ignoring all changes from downstream
git checkout downstream
git merge --squash tmp # apply changes from tmp but not as merge.
git rev-parse upstream > .git/MERGE_HEAD #record upstream 2nd merge head
git commit -m "rebaselined thebranch from upstream" # make the commit.
git branch -D tmp # deleting tmp
git merge --strategy=theirs模拟#3
这个blog post mentions:
git merge -s ours ref-to-be-merged
git diff --binary ref-to-be-merged | git apply -R --index
git commit -F .git/COMMIT_EDITMSG --amend
有时您确实想这样做,并不是因为您的历史记录中有“废话”,而是可能是因为您想更改公共存储库中的开发基线,因此应该避免变基 .
git merge --strategy=theirs模拟#4
(同一篇博文)
或者,如果您想保持本地上游分支可快速转发,一个潜在的折衷办法是理解对于 sid/unstable,上游分支可以不时重置/重新设置(基于事件在上游项目方面最终无法控制)。
这没什么大不了的,使用这个假设意味着很容易将本地上游分支保持在只需要快速更新的状态。
git branch -m upstream-unstable upstream-unstable-save
git branch upstream-unstable upstream-remote/master
git merge -s ours upstream-unstable
git diff --binary ref-to-be-merged | git apply -R --index --exclude="debian/*"
git commit -F .git/COMMIT_EDITMSG --amend
git merge --strategy=theirs模拟#5
(由Barak A. Pearlmutter提议):
git checkout MINE
git merge --no-commit -s ours HERS
git rm -rf .
git checkout HERS -- .
git checkout MINE -- debian # or whatever, as appropriate
git gui # edit commit message & click commit button
git merge --strategy=theirs模拟#6
(同Michael Gebetsroither提议):
Michael Gebetsroither 插话,声称我在“作弊”;) 并给出了另一种使用低级管道命令的解决方案:
(如果仅使用 git 命令无法实现,那就不是 git,git 中所有带有 diff/patch/apply 的东西都不是真正的解决方案;)。
# get the contents of another branch
git read-tree -u --reset <ID>
# selectivly merge subdirectories
# e.g superseed upstream source with that from another branch
git merge -s ours --no-commit other_upstream
git read-tree --reset -u other_upstream # or use --prefix=foo/
git checkout HEAD -- debian/
git checkout HEAD -- .gitignore
git commit -m 'superseed upstream source' -a
git merge --strategy=theirs模拟#7
必要的步骤可以描述为:
- 用上游替换你的工作树
- 将更改应用到索引
- 将上游添加为第二个父项
- 提交
命令git read-tree 用不同的树覆盖索引,完成第二步,并具有更新工作树的标志,完成第一步。提交时,git 使用 .git/MERGE_HEAD 中的 SHA1 作为第二个父级,因此我们可以填充它以创建合并提交。因此,这可以通过以下方式完成:
git read-tree -u --reset upstream # update files and stage changes
git rev-parse upstream > .git/MERGE_HEAD # setup merge commit
git commit -m "Merge branch 'upstream' into mine" # commit