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.
In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With