I have two branches: A
and B
.
A
's commit history:a <- b <- c
;B
's commit history:a <- h <- i
;
Assume that there is only one file here.
- In commit
b
, I adds some texts like "foo". - In commit
c
, I adds some texts like "bar". - Then I
git cherry-pick c
onB
branch. I thoughtcherry-pick
will only pick the changes inc
to branchB
. However, it will add bothfoo
andbar
to branchB
. Which is obviously not what I want.
Therefore, cherry-pick
will pick all the changes of those files touched in commit c
since the ancestor commit a
. Is that right? What if I only want to pick the diff from b
to c
and apply it onto i
?
Update the exact steps
- Init a git repo;
Add file
test.txt
and issue the first commitinit commit
.test.txt
is now:first line second line
Create a new branch
dev
but stay in branchmaster
;Add
added in commit b
to the file and issue the commitb
.test.txt
is now:first line added in commit b second line
Add
added in commit c
to the file and issue the commitc
.test.txt
is now:first line added in commit b added in commit c second line
Check out
dev
branch and issue the commith
.test.txt
is now:first line second line adding by commit h
git cherry-pick <commit c SHA1 ID>
to cherry-pick commitc
onto commith
.The conflict message:
index 6a8dc57,594c6ec..0000000 @@@ -1,4 -1,4 +1,9 @@@ first line ++<<<<<<< HEAD ++======= + added in commit b + added in commit c ++>>>>>>> 06ce9b1... commit c adding another line second line + +adding by commit h
See?
cherry-pick
also brings the changed in commitb
.
Thanks!
cherry-pick
means. But I am just confused that it is not equal to getting the diff of that commit and applying the diff to the target branch. It also contains contents NOT in that commit. – Dovecotegit show a
(replacinga
with the relevant commit SHA obviously) to inspect the changes that commit will actually introduce to see why you might be getting deltas from commitb
. You can always runcherry-pick
with the-n
flag to stop and manually trim away unwanted changes as well. – Kyanize