简体   繁体   English

git 修补分支子范围的最佳方法是什么?

[英]What is the best way to git patch a subrange of a branch?

In Subversion, it is easy to merge a range of changesets/diffs from a branch using "svn merge -ra:b mybranch".在 Subversion 中,使用“svn merge -ra: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.但是在 git 中,我发现只能从一个分支中挑选一个单一的提交来将该补丁应用到我当前的工作分支。 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?所以我想知道是否有一种快速的方法可以将 bugfix 分支中两个标签之间的所有提交一次性应用到我当前的主分支?

The easiest way to perform the action that you are looking for is with git rebase .执行您正在寻找的操作的最简单方法是使用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.假设标签 A 是您要选择的补丁系列所基于的提交,而标签 B 是该系列中最后一个补丁的提交。 Also, assume that br is the name of the current branch and the branch where the new patch series should be applied.此外,假设 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

The easiest way I've found to do this is:我发现最简单的方法是:

git cherry-pick starthash..endhash

Note that the two dots have no spaces separating them from the hash labels.请注意,这两个点没有将它们与散列标签分开的空格。 Also, this will not cherry-pick starthash , but rather everything after starthash up to and including endhash .此外,这会不会摘樱桃starthash之后,而是一切starthash直至并包括endhash To include starthash do git cherry-pick starthash^..endhash .要包含starthash请执行git cherry-pick starthash^..endhash

As far as I know, you can't git merge this way.据我所知,你不能以这种方式git merge Merge is intended to join two branches which have a history in common, not for picking up several commits or patch series. Merge 旨在加入具有共同历史的两个分支,而不是为了获取多个提交或补丁系列。 I feel like cherry-picking is fundamentally what you're asking for.我觉得樱桃采摘基本上是你所要求的。

You can use git cherry (not cherry-pick !) to find out which commits should be inserted into your branch, then git cherry-pick them.您可以使用git cherry (而不是cherry-pick !)来找出应该将哪些提交插入到您的分支中,然后使用git cherry-pick 来确定它们。 You can also explicitly ask git cherry-pick to record the origin of those commits in case you're cherry-picking from a public branch.您还可以明确要求git cherry-pick记录这些提交的来源,以防您从公共分支中挑选。 This is probably the best way to deal with this problem.这可能是处理此问题的最佳方法。 (Another could have been to export them via git format-patch and 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 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.当然,您可以在不公开代码的情况下开发闭源软件时执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM