How does cherry pick and return work?

I am trying to understand what merging and forwarding are from the point of view of given operations in mathematics.

Hereinafter, “-” means diff (similar to accepting a difference in mathematics, but “AB” means those that are indicated in A but not in B, and minus those that are indicated in B not in A), and “+” means patch (i.e., taking a disjoint union in math. I haven't used patch before, so I'm not sure).

From version control with Git, with Loeliger, 2ed

  • The git cherry-pick commit command applies the changes introduced by the named commit in the current branch. He will introduce a new, clear fixation. Strictly speaking, using git cherry-pick doesnt change the existing story in the repository; instead, he adds stories.

    enter image description here

    enter image description here

    Is it correct that F '= (FB) + Z?

  • The git revert commit command is essentially similar to the git commit cherry-pick command with one important difference: it applies the opposite to this commit. Thus, this command is used to introduce a new commit, which cancels the consequences of the commit.

    enter image description here

    enter image description here

    Is it correct that D '= G - D?

+6
source share
2 answers

cherry pick

Is it correct that F '= (FB) + Z?

No, it will also lead to the changes that have been made to C , D and E.

git-cherry-pick works by isolating the unique commit changes that will be selected by the cherry (i.e., FE in this example, ignoring additional ancestors, including the merge base) and apply them to the target.

This is not done using the patch application, but using a three-way merge algorithm - the parent commit element, which will be selected by cherry, will be used as a common ancestor, and the commit will be selected by cherries, there will be one side of the merge, with the goal, like on the other side . The result of this is the changes that were included in the cherry picker and in the target.

For example, if E is the parent fixation element selected from cherry and its contents (acting as a common ancestor):

 Line 1 Line 2 Line 3 Line 4 Line 5 

For example, if F is the fixation selected by the cherry and its contents:

 Line 1 Line 2 Line Three Line 4 Line 5 

And the goal of the cherry pick Z is:

 LINE 1 Line 2 Line 3 Line 4 Line 5! 

Then the results of a three-way merger (with annotations about where each line came from):

 LINE 1 Line 2 Line Three Line 4 Line 5! 

Revert

Is it correct that D '= G - D?

Yes, roughly speaking. Changes that were unique to D were removed from G. Like git-cherry-pick , git-revert is implemented using a three-way merge, although this time the commit to return is considered a common ancestor, one side is the current commit, and the other side is the commit to return the parent.

This will mean that when the line is identical between the commit and return and the current commit, a row from its parent will be selected instead.

If the contents of D , the commit to return acts as a common ancestor, and its contents:

 Line 1 Line 2 Line THREE Line 4 Line FIVE 

And the contents of C ( D ):

 Line 1 Line 2 Line 3 Line 4 Line 5 

And the contents of G were changed additionally, and its contents:

 Line One Line 2 Line THREE Line 4 Line FIVE 

Then the results of the tripartite merger will be:

 Line One Line 2 Line 3 Line 4 Line 5 

This is the result of using unique strings in the parent C and target G.

Merge commits

Like torek notes (below), since these mechanisms include the use of a parent commit, they break when there is more than one parent commit. (That is, the transaction in question is a merger and has several parents.) In this case, you need to tell git which parent should consider (using the -m flag).

Conflicts

Of course, any of these mechanisms can cause conflicts. For example, if the current conflict has changed even more, you will have to resolve the conflicts. For example, if in the return example (above), the subsequent commit also changed line 5, then G :

 Line One Line 2 Line THREE Line 4 LINE FIVE! 

Then a conflict arose. The working directory (combined file) will look like this:

 Line One Line 2 Line 3 Line 4 <<<<<<< LINE FIVE! ======= Line 5 >>>>>>> 

And you will need to decide whether you want the initial change ( Line 5 ) or the newest change ( LINE FIVE! ).

+5
source

Its very easy to understand like this:

cherry-pick

select which commits (from any branch or may even be a free commit) select this commit and put it in my current branch, in other words - make any commit from anywhere in the repository, add it to my branch


revert

Cancel commit. it will “undo” any changes made to the commit, canceling them if you know what the patch is, so you can see it as a sign change in the patch - becommig + and vice versa. your changes are “reverted” and the changes are discarded.

The git revert command cancels the taken snapshot.

But instead of removing the commit from the project’s history,
it calculates how to undo the changes made by the commit, and adds a new commit with the received content.

This prevents Git from losing history , which is important for the integrity of your change history and for reliable collaboration.


Is it correct that F '= (FB) + Z?

It just means that now in the lower branch you also have a patch that was created in commit F, your lower branch contains its changes + changes that were made in commit F (and only ) no other is fixed next to F)


Is it correct that D '= G - D?

Not really - it means that now you have a D commit, and after several commits you have the cancellation of this commit, you still have 2 commits in the repository, but the code will not change (change + cancel to 2 separate commits)

+2
source

All Articles