0

Suppose you branch off a branch newfeature from the master branch at commit C0. When the feature is ready, you merge it back to the master branch. Usually I then delete the newfeature branch.

Someone asks you for a patch file for that particular feature based on the commit C0, when you branched off the newfeature branch. So you don't want the patch to include any other feature which has been merged to the master branch.

Is there a way to get this after merging and deleting the newfeature branch? If not I guess that would be a potential scenario where you wouldn't delete feature branches, right?

To push it further. Suppose we branch the newfeature branch off the master branch some commits later, say C3, develope the feature and merge it back. Someone asks for a patch file of that particular feature but based on a the commit C0. Is there a way to get that patch file?


Example of the potential situation:

M0-M1-M2---M3--M4-M5
    \  \-G0-G1-/ /
     \-F0-F1-F2-/
  • You can use a simple diff between the commit from which you branched off of, and the commit where you merge in the feature branch. I'm assuming that your feature branch only contains a single feature, however. If it contains multiple features, then it's not really a good, isolated feature branch. Also, if your branch and merging the same feature branch into and out of the master branch (like in your last example, if I'm understanding it correctly...it's a little unclear), then that's also not a good way to do a short-lived, isolated feature branch. –  Aug 06 '14 at 19:08
  • @Cupcake But a diff between the commit from which I branched off and the commit where I merged back in the feature branch, contains all commits which were done in between on the main branch, no? If I wouldn't delete the feature branch, I would simply diff between the last commit and the feature branch and the commit where I branched off... – user83983293 Aug 06 '14 at 19:14
  • Oh right, my mistake. What you really want is to diff the merge commit with the commit right before it on the master branch, but that's assuming that you haven't merged any commits from master into your feature branch. –  Aug 06 '14 at 19:23
  • I haven't. I'll try to be more precise. Suppose the master branch looks like this: M0,...,M5, where at M1 I branched off a feature branch F0,...,F3 and then merged that back to the master branch which resulted in commit M5. (In the second case there is an additional feature branch G0,G1,G2 which was branched off at M1 and merged back into M4.) Afterwards I deleted the feature branch. If I now diff M5 against M4 that would allow me to generate a patch that someone else could apply on top of M0? – user83983293 Aug 06 '14 at 19:34
  • I'm going to be honest, you'll have much better luck explaining your situation to other people [if you just make a commit graph](http://stackoverflow.com/q/25156653/456814). You can even add the output of `git log --oneline --graph --decorate` if you want. –  Aug 06 '14 at 19:40
  • Sorry. I tried to do so (edited the original question accordingly). Hope that helps? So F0-F1-F2 is one feature branch and G0-G1 the other. Is it possible to generate a patch files for non-git users who currently use the code at stage M0 so that they can apply a patch just to add only the feature F to their code? (e.g. if they don't want to have feature G). I guess, if I didn't delete the F-branch I would rebase F2 to M0 and build a patch file from that.. – user83983293 Aug 06 '14 at 19:56
  • Why are you trying to apply a patch to M0 if M1 is where you branched off of? M1 is where you should apply a patch. Applying to M0 *might* work on this occasion (as in not create conflicts), but in general, you have a high chance of creating conflicts by applying a diff-patch to a commit that's not actually the base. –  Aug 06 '14 at 19:56
  • `git diff M5^ M5` and `git diff M1 M5` should both get you the same diff-patch that you can then apply to M1. Like I said, you can attempt to apply it to M0, but you have a high chance of creating conflicts like that. –  Aug 06 '14 at 19:57
  • The patch should contain M1-F0-F1-F2. – user83983293 Aug 06 '14 at 19:58
  • Right, the commands I gave you will generate that patch. Or did you also want to retain the commit messages along with the changes? If you did, then `git format-patch` is the command that you want to use to generate those patches. –  Aug 06 '14 at 19:58
  • So `git diff M0 M5` would only contain feature F and `git diff M0 M4` only the feature G? I thought the former would contain feature G and commits M2-M4 as well.. So, I guess if I wouldn't delete feature branches I could do `git diff M0 F2` to obtain a patch that only adds feature F on top of M0. – user83983293 Aug 06 '14 at 19:59
  • Wait, no, sorry, I got all the commits mixed up because it wasn't clear at first that you were trying to apply a patch to M0. You were right the first time, you want `git diff M0 F2` for that branch. But that will generate a patch that can only be applied as a single commit. Did you want to generate patches for each individual commit? Because again, the command for that is `git format-patch`. –  Aug 06 '14 at 20:02
  • One single commit would be okay, as other's are not using git. I just want to provide a patch file to them. But wait, there is no way I can do it, once I've deleted the feature branch? So this brings me back to my orignial question: this would be a scenario where I'd better keep feature branches? – user83983293 Aug 06 '14 at 20:03
  • Why would you need to keep your feature branch? It's just a bookmark on the commits. You still have the commits in your development history (of the master branch), even if you throw away the bookmark. Just use the commit shas. –  Aug 06 '14 at 20:05
  • Ah, then there is the problem coming in. I thought of deleting a branch as removing it and only keeping the code and results at M5. So `git diff M0 F2` or `git diff M0 G1` would provide the desired patches? So I just need to remember the last commit in the particular feature branch and run the git-diff (while the F0,G0,etc commits are still in the main branch after deleting the feature branches). – user83983293 Aug 06 '14 at 20:09
  • Yes, the `git diff` commands should give you the patches that you're looking for. You should make a new branch off of M0 and `git apply` the patches just to double check. `git diff` can take commit shas as arguments. Deleting a branch doesn't delete (merged) commits, it only throws away the bookmark. As long as you have another bookmark that eventually leads back to those commits (e.g. the master branch), then Git won't garbage collect the commits. –  Aug 06 '14 at 21:00

0 Answers0