4

need some help! Have problems with cherry-pick. I create *.sh file with the following sequencing (execute in empty folder!):

 #!/bin/sh

git init

echo "init" >> file.txt && git add . && git commit -m "commit init" 

git checkout -b develop

for i in $(seq 1 8)
do 
    echo $i >> file.txt && git add . && git commit -m "commit $i" && git tag v$i 
done

git checkout master

git checkout -b some_other_branch

git cherry-pick v5

If you execute it, you find it in conficting state during the cherry-pick. That fact is ok for me, but if you open file file.txt you find there all changes 1,2,3,4,5, not as I expected only 5.

init
<<<<<<< HEAD
=======
1
2
3
4
5
>>>>>>> 0ea6510... commit 5

But I want to see only 5 as a diff.

init
<<<<<<< HEAD
=======
5
>>>>>>> 0ea6510... commit 5

How can I manage to do it? Maybe I shall set diff some additional properties?

kirill
  • 109
  • 5
  • 1
    You are effectively appending `$i` to the `file.txt`, so `v5`'s version of it contains 6 lines, which is 5 lines different from the master: the diff is correct. – Kenney Nov 30 '15 at 10:31
  • I doubt `git cherry-pick v5` is picking from the tag v5 successfully,instead picking entire list of commits. Could you try doing it with the commit name once manually instead of v5 and check. – Naman Nov 30 '15 at 10:36
  • To @Kenney: I understand that it is correct. I didn't write it is not! i just ask how to make more...user friendly, to say so. Because as I wrote I expect diff to be shown in conflict! And the diff for commit v5 - is a string number 5. – kirill Nov 30 '15 at 11:25
  • To @nullpointer: actually there is no need in tag. But I cherry-pick diff from commit, not tag – kirill Nov 30 '15 at 11:35
  • You are getting the diff for *all commits* between `master` and `v5`, so, commit 2, 3, 4, 5 and 6, adding the lines 1, 2, 3, 4 and 5. If you did `git checkout v4 -b somebranch; git cherry-pick v5` you would have a diff for 1 commit (which doesn't produce a conflict here, probably because it's only 1 commit difference) . I don't use cherry-pick myself, but `git rebase -i`. – Kenney Nov 30 '15 at 11:48
  • @Kenney Well I use cherry-pick and there is clear about changes you take with cherry-picking commit: "Given one or more existing commits, apply the change each one introduces, recording a new commit for each." [link](https://git-scm.com/docs/git-cherry-pick) And as I can understand the question is about how git shows us the diff. Becasuse in case there were no conflict it applies only v5 diff to my HEAD. – kirill Nov 30 '15 at 12:10
  • 1
    I see what you mean, but what they mean with *"Given one or more existing commits"* is the commits you specify as commandline arguments to `git cherry-pick`. You pass the `v5` commit, which introduces the changes of adding 5 lines to `test.txt`, relative to `master` (or `some_other_branch`). Try this instead: `git cherry-pick v1 v2 v3 v4 v5`. – Kenney Nov 30 '15 at 12:18
  • @Kenney "You pass the v5 commit, which introduces the changes of adding 5 lines to". That is not correct. v5 add only one line! And yes: `git cherry-pick v1 v2 v3 v4 v5` add 5 lines. I need only one line adding in commit v5 – kirill Nov 30 '15 at 12:21
  • 1
    What I meant was that `v5` records the contents of `test.txt` as having 6 lines total, which adds 5 lines relative to master (`git diff master v5`). The problem here is that the change that `v5` introduces (relative to `v4`) is adding a sixth line (`5`) to the file. The `master` branch only knows about the file having 1 line. You could easily cherry-pick `v1` because it adds line number 2, but `v2` and up won't work without conflict, because `v2` adds a 3rd line to a 1 line file, the same way that `v5` adds a 6th line to a 1 line file - git doesn't know what to do with the missing lines 2..5. – Kenney Nov 30 '15 at 12:42
  • @Kenny is right. @kirill, please remember that `git` keeps track of the project development **history**. Hence, `v5` doesn't make sense without the commits that have come before it. – houtanb Nov 30 '15 at 14:14

0 Answers0