Git checkout: detailed meaning of "their" and "ours"

The git documentation says:

- ours - theirs When checking paths from the index, check step 2 (ours) or 3 (their) for unrelated paths.

What does “stage number 2” and “stage number 3” mean during the merger, reinstallation and cherry pick? Is there a way to request theses “steps” before running a command to make sure it returns the correct version?

+5
source share
2 answers

They are documented (although not everything is obvious, I think) in the gitrevisions documentation :

A colon, optionally followed by a stage number (0 to 3) and a colon followed by a path, names the blob in the index on that path. An invalid stage number (and the colon that follows) indicates the entry for stage 0. During the merge, stage 1 is a common ancestor, the second stage is a version of the target branches (usually the current branch), and stage 3 is the version from the branch, which unites.

To do this, you need to add knowledge about how git rebase and git cherry-pick .

The usual cherry pick is clearly defined: "ours" is the HEAD version, i.e. the branch you were on (and still have), and "them" is a commit that you are actively collecting. When you choose a single commit in a cherry-like fashion, all of this is pretty obvious: stage number 1 is a common ancestor, stage number 2 is the version from the tip of your current branch, and stage number 3 is the version you chose for cherry.

If you cherry-pick a series of commits, this is still true, it is just iteratively true. Say you, for example, have chosen three vitamins. Git is just three times in a row. During the first set of cherries, step number 2 is the end of your branch, and step number 3 is the version with the first commit selected in cherry color. After this completes the cherry grip, Git commits again, pushing the tip of your branch. Then, during the second set of cherries, stage number 2 is the tip of your branch, which is the fixation of your first cherry pick, and stage number 3 is an option from the second commit taken. This is repeated again for final fixation. Each time stage number 3 is "their" version.

Rebase, however, is a bit more complicated. Inside, it starts with the fact that you get to a new anonymous branch ("separate head"). Then it runs git cherry-pick to select each commit from your source branch. This means that "ours" is a separate version of HEAD, and "theirs" is a version from your original branch. Like cherries, it repeats iteratively for each commit you want to select (literally in the case of interactive permutation, where you edit pick lines). Once the overflow is over, Git simply moves the branch label around, so the new anonymous branch you just created is your code.

In short, you may think of rebase as a “rearrange of our / their settings”, but this is an exaggeration. Perhaps it would be more accurate to say that the second stage is your new, combined code, and stage 3 is your old code.

+5
source

The git documentation for merge (as well as several other places) explains that the index file writes up to three versions or steps:

For conflicting paths, the index file writes up to three versions: stage 1 stores the version from a common ancestor, stage 2 from HEAD and the third stage from MERGE_HEAD (you can check the stages with git ls-files - and). Work tree files contain the result of the "merge" program; those. trilateral merger results with familiar conflict markers <<=== →>.

Here is a diagram showing that the three steps are in a typical git merge:

 Common Ancestor -> C1 --- C2 <- MERGE_HEAD (Stage 3) (Stage 1) \ --- C3 --- C4 <- HEAD (Stage 2) 

This assumes that a branch whose HEAD is C4 merges back into a branch ending in C2 .

As indicated in the documentation, you can view the steps by typing:

 git ls-files -u 
+3
source

Source: https://habr.com/ru/post/1238063/


All Articles