简体   繁体   中英

Why is git cherry-pick conflict including changes from a previous commit

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?

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 Basics

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.

Cherry-picking

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 (ie, 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 same file 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM