git 修补分支子范围的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/509859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to git patch a subrange of a branch?
提问by jscoot
In Subversion, it is easy to merge a range of changesets/diffs from a branch using "svn merge -r a:b mybranch". But in git, I found it is only possible to cherry-pick a single commit from a branch to apply that patch to my current working branch. So I am wondering if there is a fast way to apply all the commits in one swoop between two tags in a bugfix branch to my current master branch?
在 Subversion 中,使用“svn merge -ra:b mybranch”可以轻松地从一个分支合并一系列变更集/差异。但是在 git 中,我发现只能从一个分支中挑选一个单一的提交来将该补丁应用到我当前的工作分支。所以我想知道是否有一种快速的方法可以将 bugfix 分支中两个标签之间的所有提交一次性应用到我当前的主分支?
回答by CB Bailey
The easiest way to perform the action that you are looking for is with git rebase
. Here's a recipe. Assume that tag A is the commit on top of which the patch series that you want to select is based and that tag B is the commit of the final patch in the series. Also, assume that br is the name of the current branch and the branch where the new patch series should be applied.
执行您要查找的操作的最简单方法是使用git rebase
。这是一个食谱。假设标签 A 是您要选择的补丁系列所基于的提交,而标签 B 是该系列中最后一个补丁的提交。另外,假设 br 是当前分支的名称和应该应用新补丁系列的分支的名称。
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the br branch to the head of the new patchset
git branch -f br B
# Rebase the patchset onto tmp, the old location of br
git rebase --onto tmp A br
回答by Grant Diffey
The easiest way I've found to do this is:
我发现最简单的方法是:
git cherry-pick starthash..endhash
note that the two dots have no spaces seperating them from the hash labels.
请注意,这两个点没有将它们与散列标签分开的空格。
回答by Eduard - Gabriel Munteanu
As far as I know, you can't git mergethis way. Merge is intended to join two branches which have a history in common, not for picking up several commits or patch series. I feel like cherry-picking is fundamentally what you're asking for.
据我所知,你不能以这种方式git merge。Merge 旨在加入具有共同历史的两个分支,而不是为了获取多个提交或补丁系列。我觉得采摘樱桃从根本上是你所要求的。
You can use git cherry(not cherry-pick!) to find out which commits should be inserted into your branch, then git cherry-pickthem. You can also explicitly ask git cherry-pickto record the origin of those commits in case you're cherry-picking from a public branch. This is probably the best way to deal with this problem. (Another could have been to export them via git format-patchand then importing them with git-am/git-apply, but that would likely be slower, plus it won't record the origin of the commits.)
您可以使用git cherry(而不是cherry-pick!)找出应该将哪些提交插入到您的分支中,然后使用git cherry-pick 来确定它们。您还可以明确要求git cherry-pick记录这些提交的来源,以防您从公共分支中挑选。这可能是处理此问题的最佳方法。(另一个可能是通过git format-patch导出它们,然后使用 git-am/git-apply 导入它们,但这可能会更慢,而且它不会记录提交的来源。)
EDIT: "Public" (branch) should be understood as something not subject to history editing. Of course, you can do this when developing closed-source software without the code being public.
编辑:“公共”(分支)应该被理解为不受历史编辑限制的东西。当然,您可以在不公开代码的情况下开发闭源软件时执行此操作。