如何为分支中的所有提交生成补丁?

如何通过只知道分支名称来为给定分支中的所有提交生成补丁?

这个步骤是一个复杂的工作流程的一部分,所有这些都是自动化的。因此,不能要求某人手动确定分支中的第一次提交。

请注意,任何依赖于 reflog 的操作都不是一个选项,因为分支中的更改不是在本地进行的。

58363 次浏览

If you know from which branch your "given branch" has been created, then making a patch is easy:

git diff master Branch1 > ../patchfile
git checkout Branch2
git apply ../patchfile

(and you can generate a patch applicable without git too)

But finding the right "creation commit" of a branch can be complex: see "Finding a branch point with Git?"

The OP akirekadu used:

git format-patch $(git merge-base --fork-point master)..branchB

You can see it used in "git diff between working copy and branch base"

legends2k adds in the comments:

One can verify the generated patch with git apply --stat patchfile
This won't apply, but give the details of the patch.


Warning: AGP notes in the comments that:

$(git merge-base --fork-point master)..branchB command may not find the correct branch root revision every time


ijoseph points out in the comments

Incidentally, Phabricator handles this kind of stuff seamlessly by automatically generating those patch files for each push.

IF you have already committed and pushed your changes are now you want to create a patch file.

This will create a patch in 1 file even if you have multiple commits in that branch

STEP1: Do checkout the branch whose patch you want like any feature/bug branch
ex: git checkout <branch>

STEP2: Now this will create a patch for all commits diff with master branch with your checkout branch i.e the above branch you have checkout.

git format-patch master --stdout > mypatch.patch