Why are there additional changes in the git cherry conflict?

In a case git cherry-pickleading to conflict, why does Git offer more changes than just from this commit?

Example:

-bash-4.2$ git init
Initialized empty Git repository in /home/pfusik/cp-so/.git/
-bash-4.2$ echo one >f
-bash-4.2$ git add f
-bash-4.2$ git commit -m "one"
[master (root-commit) d65bcac] one
 1 file changed, 1 insertion(+)
 create mode 100644 f
-bash-4.2$ git checkout -b foo
Switched to a new branch 'foo'
-bash-4.2$ echo two >>f
-bash-4.2$ git commit -a -m "two"
[foo 346ce5e] two
 1 file changed, 1 insertion(+)
-bash-4.2$ echo three >>f
-bash-4.2$ git commit -a -m "three"
[foo 4d4f9b0] three
 1 file changed, 1 insertion(+)
-bash-4.2$ echo four >>f
-bash-4.2$ git commit -a -m "four"
[foo ba0da6f] four
 1 file changed, 1 insertion(+)
-bash-4.2$ echo five >>f
-bash-4.2$ git commit -a -m "five"
[foo 0326e2e] five
 1 file changed, 1 insertion(+)
-bash-4.2$ git checkout master
Switched to branch 'master'
-bash-4.2$ git cherry-pick 0326e2e
error: could not apply 0326e2e... five
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
-bash-4.2$ cat f
one
<<<<<<< HEAD
=======
two
three
four
five
>>>>>>> 0326e2e... five

I was expecting only a "five" between conflict markers. Can I switch Git to the expected behavior?

+4
source share
2 answers

Before we get further, let's draw a fix graph:

A   <-- master (HEAD)
 \
  B--C--D--E   <-- foo

or so that you can compare, here's the Git way draws it:

$ git log --all --decorate --oneline --graph
* 7c29363 (foo) five
* a35e919 four
* ee70402 three
* 9a179e6 two
* d443a2a (HEAD -> master) one

(Note that I included your question in the command sequence that I added, my hashes are of course different from yours.)

Cherry-pick - a peculiar form of merging

, , , git cherry-pick . - .

( - , ) git merge other. Git , other, , , . , :

          o--o--L   <-- ours (HEAD)
         /
...--o--B
         \
          o--o--R   <-- theirs

commit B ( ).

, Git git diff s, commit L , R ( ). :

git diff --find-renames B L   # find what we did on our branch
git diff --find-renames B R   # find what they did on theirs

Git , B, , L, R. - , "" . , "" .

, Git , . , "merge" .

, Git - , , . - , .

E. , Git () commit D , A / L commit E R. Git :

git diff --find-renames D A   # what we did
git diff --find-renames D E   # what they did

, : , two, three four. , , : five.

merge.conflictStyle

- , , 😅 - merge.conflictStyle diff3. , , <<<<<<< .., Git , |||||||:

one
<<<<<<< HEAD
||||||| parent of 7c29363... five
two
three
four
=======
two
three
four
five
>>>>>>> 7c29363... five

, Git , , .

, , commit E, - "" commit A. , . , . Git, , .

: script

#! /bin/sh
set -e
mkdir t
cd t
git init
echo one >f
git add f
git commit -m "one"
git checkout -b foo
echo two >>f
git commit -a -m "two"
echo three >>f
git commit -a -m "three"
echo four >>f
git commit -a -m "four"
echo five >>f
git commit -a -m "five"
git checkout master
git cherry-pick foo
+2

... , , " , , ", " , " hunk ", , ".

git , , " " , , , .

a
b
c
d
e
f
g
h
i

,

x -- A <--(master)
 \
  C -- E -- G <--(branch)

( commit A, ..), , , , - git, , cherry-pick E to master. ( , , .)

, - , , . , - " " , - . , , , , . ( , , : , , , .)

:

1) , , , , , ""

2) - - .

+1

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


All Articles