7/29/2023 0 Comments Git cherry pick strategyStart with a commit A, where the line in question is apple. But consider this three-branch scenario: apple In this case, there haven’t been many changes, and there are only two branches involved, and hopefully there aren’t too many other conflicts in the merge (so that the person resolving the merge hasn’t gotten tired and burnt out), so the chance of a correct resolution are pretty good. You have to hope that whoever resolves this merge conflict remembers the history of this line, or can access the team’s knowledge of this line of code to understand that the correct resolution it to accept the changes in the feature branch rather than the one in the master branch. We’ve been using dotted lines in our diagrams to emphasize that the cherry-pick relationship is all in our heads, and not actually recorded anywhere in the commit graph. The conflict occurred because the cherry-picked changes were subsequently changed again by one of the branches. The base of the three-way merge contains apple, the incoming feature branch has cherry and the existing master branch has berry. This time, when it’s time to merge the feature branch back into the master branch, there is a merge conflict. Whatever the reason, the commit graph now looks like this: apple This could be because the person who made the original commit F2 found an improvement (cherries are on sale right now), or maybe they made a larger change that happened to require switching from berries to cherries. It is also relatively uncommon in an active code base.Ĭonsider this alternate timeline: After the cherry-pick, additional commits M3 to the master branch and F3 to the feature branch are made, but this time commit F3 changes the line in question to cherry. It’s time to merge back, and since the line in question is the same in both branches, the merge is trivial, and the result in the final merged result is berry. Neither of these commits affected the line in question, so the line is still the word berry. You made another commit M3 to the master branch and another commit F3 to the feature branch. Time passes, more commits occur, and your commit graph looks like this: apple You then make some commit F2 in the feature branch that changes the line in question from apple to berry, and you cherry-pick commit F2 into the master branch as M2. These changes don’t affect the line in question, so it still says apple. From that common ancestor, the two branches diverge: Commit F1 happens on the feature branch, and commit M1 happens on the master branch. You have some common ancestor A, and in the commit, the line in question is the word apple. (Eventually, I’ll stop drawing dotted lines, too.) This dotted line doesn’t really exist in the repo, but I’m drawing it to help express the chronology. Each commit will be annotated with the contents of that one line.įor the purpose of illustration, I’m using a dotted line to denote cherry-picks. And for simplicity’s sake, let’s say that the commit being cherry-picked is a one-line change to a single file. Let’s call them the master branch and the feature branch. In order to cherry-pick, you need two branches, one to be the donor and one to be the recipient. It’s a tall order, but I’ve been meaning to write this up for a while, and what’s gotta get done gotta get done. This is the start of a series that will begin by explaining why cherry-picking is bad, continue by explaining why cherry-picking is worse, then try to talk you down from the ledge by showing how to get the same effect as a cherry-pick but with merging, showing how to apply that technique to the case where you need to do a retroactive merge, and wrap up by showing how to apply that technique to the case where you already made the mistake of cherry-picking and want to fix it before something bad or worse happens. Sometimes it’s a neutral idea, but I haven’t yet found a case where it’s actually good. Cherry-picking is a common operation in git, and it’s not a good idea.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |