Julia Evans

How git cherry-pick and revert use 3-way merge

Follow
The author, initially misunderstanding git cherry-pick as simply applying a patch, delves into its actual workings, revealing a more nuanced process involving a "3-way merge." The misconception arose when attempting to resolve merge conflicts using the patch method, which failed, unlike the expected behavior of git cherry-pick. Further investigation into git's source code revealed that cherry-pick utilizes a 3-way merge, a concept unfamiliar to the author at the time. This prompted exploration of 3-way merges, which, in essence, involve merging two files by comparing them to their original version (the base). This concept extends to applying patches in git, where the file versions before and after the commit, along with the current file, constitute the three versions used in the merge. Cherry-pick, rebase, and revert all leverage this 3-way merge strategy, differing primarily in the order and interpretation of the base and target versions. The author coins the term "3-way patch" to describe this technique, emphasizing its advantage in providing more context for merging compared to traditional patches. While git apply typically handles traditional patches, it also offers a --3way flag for performing 3-way merges. This exploration highlights the cleverness of using 3-way merges for applying patches in git, offering a unified approach for various operations while remaining transparent to users who can stick to the familiar concept of "applying a patch." The author acknowledges the complexity of merging in git, hinting at concepts like recursive merges and multiple merge algorithms, with the book "Building Git" suggested for further exploration. Finally, the author expresses appreciation for the elegant design of git's patching mechanism, praising its intuitiveness and effectiveness.
favicon
jvns.ca
jvns.ca
Create attached notes ...