git cherry-pick cherry-pick merge

git cherry-pick can select one or several commit(s) in a branch to operate. For example, suppose we have a stable version branch called v2.0, and a development version branch v3.0. We cannot directly merge the two branches, which will cause confusion in the stable version, but want to add v3. 0 to v2.0, you can use cherry-pick here. It is to resubmit an existing commit;

1, First get the commit_id you want to merge through git log and record it

commit df8960346dc803a5c1485551c5d109fd7dda6ae6
Author: efg
Date:   Mon Apr 23 18:48:31 2018 +0800

    测评

commit a6a31d169d8b39b4cc3bd1da6fa3184fdb19321c
Author: xwz
Date:   Mon Apr 23 18:37:34 2018 +0800

    空tag  重提交

commit 3a683f2787f011506ee11ce5cfe342f67f337054
Merge: 29cac5d 937d68b
Author: abc
Date:   Mon Apr 23 18:06:41 2018 +0800

    Merge branch 'master' into vanke-fixbug-20180423-1

commit 29cac5d01424816fa5b4a61a5c1941d9d94ea970
Author: xyz
Date:   Mon Apr 23 14:03:21 2018 +0800

    二维码姓名不一致

2, switch the main branch;

3, Use the git cherry-pick command to merge the branch you want to merge into the master branch;

git cherry-pick <commit_id>

A. If it goes well, it will be submitted normally . result:

Finished one cherry-pick.
# On branch old_cc
# Your branch is ahead of 'origin/old_cc' by 3 commits.


B. If there is a conflict during the cherry-pick process

Automatic cherry-pick failed.  After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
and commit the result with: 

        git commit -c df8960346dc803a5c1485551c5d109fd7dda6ae6

For example: There is a master branch master, which has made two commits (m0 and m1). At this point, a new branch, develop, was opened for development, and three commits were made (d0, d1, and d2). If you only want to merge the d2 submission into the main branch master, I checked a lot of information and said that the cherry-pick command should be used, so

git checkout master
git cherry-pick d2的commit哈希码

But at this time, there will be a conflict, which requires the user to edit it manually. Only conflict can be resolved at this point. The reason for not being able to cherry-pick is that the file modified by d2 has been modified in d1 (or d0), so cherry-pick does not know how to delete and add the corresponding lines, so there is a conflict. The only way is to resolve the conflict.

In addition, cherry-pick does not move the pointer to d2, cherry-pick is more like patch the changes, even if there is no conflict, the commit hash is different.


Just like a normal conflict, manually resolved


B.1 $ git status # See which files are in conflict

both modified: app/models/user.rb

B.2 $ vim app/models/user.rb # Solve it manually
B.3 $ git add app/models/user.rb

B.4 git commit -c <new commit number>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325557844&siteId=291194637