Reputation: 342
I have a staging branch where I made some commits for a feature, I need to make these commits from a master branch instead. I tried to checkout a branch from master branch using
git checkout -b branch_name
, then took the first commit that I wanted to cherry-pick from the staging branch with
git cherry-pick hash
. For some reason there is a conflict with the cherry pick but the conflict includes changes from a commit previous to the one that was cherry-picked, why is that?
Upvotes: 3
Views: 915
Reputation: 60013
TL;DR What you're seeing is directly related to how Git records and stores the history of your files compared to other VCS.
The easiest way to grasp it is by considering the concept of revision (or version).
Traditionally, version control systems record a revision of the files in your working directory by storing the differences between how the files are now and how they were in the previous revision (this is also known as the delta or Δ).
Git, on the other hand, records a revision by storing a snapshot of all the files in your working directory as they are right now.
This is best illustrated with an image used in the Pro Git book:
Here, you see how a traditional VCS only stores the differences in each file between revisions.
In Git, it looks more like this (again from the Pro Git book):
You see that every revision is associated to a snapshot of all the files in the working directory. However, the documentation states:
To be efficient, if files have not changed, Git doesn’t store the file again—just a link to the previous identical file it has already stored.
However, conceptually, you can still think of each commit pointing to a snapshot of your entire working directory.
Now, consider what happens when you cherry-pick a commit. Let's say that we want to cherry-pick the commit corresponding to Version 5
:
git cherry-pick <version-5>
Git is going to merge
the snapshot associated to the commit referenced by HEAD
(i.e., the files in your working directory) with the snapshot associated to version-5
.
Now, if version-4
modified a line in file B
(resulting in file B1
) in a way that conflicts with how file B
looks like in your working directory, you'll get a conflict. Here's the important part:
The conflict is going to happen even if
version-5
(the one you're cherry-picking) modified the samefile B
in a way that does not conflict with anything.
That's because the snapshot associated to version-5
contains file B2
, which is the result of all the modifications file B
has gone through across the previous revisions.
Upvotes: 10