A
A
Anton Dmitriev2021-01-04 13:50:33
git
Anton Dmitriev, 2021-01-04 13:50:33

Why does git cherry-pick copy the entire branch when copying a commit?

Hello!

When studying git, I encountered a misunderstanding of how it should work in case of copying branches.
There are rumors that cherry-pick takes git diff -U0 feature^ HEAD and copies it to HEAD, but I didn't succeed.

Here is a list of my commits:

spoiler

$ git log --oneline
5cab6cb (HEAD -> feature) Add D
a3f623d Add C
9b26f0e Add B
f5f7ec0 (master) Add A
449d5e5 Add .keep


There is one file in the project; in the file, for each commit, there is one addition of the line indicated in the comment.

Here is my git diff -U0 feature^ HEAD:
spoiler

[email protected] MINGW64 /k/dev/git/test_2 (feature)
$ git diff -U0 feature^ HEAD
diff --git a/script.txt b/script.txt
index 05c9ba8..aa5292a 100644
--- a/script.txt
+++ b/script.txt
@@ -6,0 +7,2 @@ C
+D
+


And here is what I get when I do a cherry-pick:
spoiler

[email protected] MINGW64 /k/dev/git/test_2 (master)
$ git cherry-pick feature
Auto-merging script.txt
CONFLICT (content): Merge conflict in script.txt
error: could not apply 5cab6cb... Add D
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'


OK. There was a conflict. But why? In the master branch, after the start of adding commits to the feature branch, no commits were made and only lines were added to the feature branch.

What's in the conflict file:
spoiler

A
<<<<<<< HEAD
||||||| parent of 5cab6cb (Add D)

B

C

=======

B

C

D

>>>>>>> 5cab6cb (Add D)


And as you can see, it's not "diff -U0 feature^ HEAD" at all, but something like "$ git diff -U0 master...feature", i.e. what cherry-pick added to me is more like what merge would do, except for the metadata that is copied.

1. Am I misunderstanding how this command should work, or did something go wrong here?
2. What is the cherry-pick algorithm?
3. If cherry-pick works the same way as merge, then maybe it's better to do for copying squash?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-01-04
@jcmvbkbc

OK. There was a conflict. But why?

Because the changes are applied like patches, given the context. And not stupidly in the forehead "insert two lines with the following content starting from line 7".
1. Am I misunderstanding how this command should work, or did something go wrong here?

Both this and that. You imagine the transfer of changes too simply, and in your case there was a conflict, just where your idea does not expect it.
3. If cherry-pick works the same way as merge, then maybe it's better to do for copying squash?

cherry-pick doesn't work like merge. merge merges entire branch history, cherry-pick applies one single change.
Try making your changes (Add A, Add B, Add C, Add D) to different files to see the difference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question