How to generate patch for all commits in a branch?
Asked Answered
A

2

104

How can I generate patch for all commits in a given branch by knowing only the branch name?

This step is part of a complex workflow all of which is being automated. Hence requiring someone to manually determine the first commit in the branch is not an option.

Note that anything relying on reflog is not an option either because changes in the branch are not made locally.

Adigun answered 15/3, 2017 at 4:3 Comment(3)
Isn't the whole point of git that it doesn't matter if the changes are made locally or not? Please elaborate on your last sentence a lotBurnaby
check --fork-point notes here git-scm.com/docs/git-merge-baseAdigun
Note that you can specify git tags in the commands, so if your automated system(s) can automatically add tags, you can create diffs between these points if you wish.Sharpeyed
M
145

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.

Messalina answered 15/3, 2017 at 5:56 Comment(12)
Let's call the branch in question 'B'. The 'source' branch from where B was created is known. However, generating a diff comparing to it will not produce correct results (ie, we need all the changes made only in this branch B) because the source branch might have changes ever since B was created.Adigun
@Adigun yes, that is why I referred in the answer to https://mcmap.net/q/12053/-finding-a-branch-point-with-git/6309: finding the current point of origin of a branch is hard.Messalina
Fair enough. I ended up going with: git format-patch git merge-base --fork-point master..branchBAdigun
@Adigun Good point. I have included your comment in the answer for more visibility, and added a reference to the fork-point option.Messalina
You would need 'git format-path ' in front of the command you added. ie, git format-patch `git merge-base --fork-point master`..branchBAdigun
One can verify the generated patch with git apply --stat patchfile; this won't apply but give the details of the patch.Groundwork
@Groundwork Thank you. I have included your comment in the answer for more visibility.Messalina
@AGP Could you ask a separate question illustrating how this answer does not address your use cse?Messalina
@Messalina It is not reuqired to asked as a separate question. It can be added that $(git merge-base --fork-point master)..branchB command may not find the correct branch root revision every time. That should be enough.Holey
Incidentally, Phabricator handles this kind of stuff seamlessly by automatically generating those patch files for each push.Signor
@Signor Thank you. I have included your comment in the answer for more visibility.Messalina
Thank you did not even know that was possible.Defile
E
7

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

Editor answered 15/7, 2021 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.