The function and usage of git cherry-pick

In the actual development process, transferring code from one branch to another is a common requirement.

At this time, there are two situations:

  • In one case, we need all the code changes of another branch , then we can use merge processing git merge;
  • In another case, we only need part of the code changes (one or more commit submissions), which can be adopted at this time cherry pick.

1 The role of cherry-pick

cherry-pick, carefully selected means to select a commit we need.

Its function: transplant the commit modification on other branches to the current branch.

2 usage scenarios

I want to add a certain function in a newly developed version instead of all the code of a certain branch to a stable version.

You can use git cherry-pick <commit-id>the command to extract the commit related to this function and merge it into the branch of the stable version.

3 How to use

Pull only one commit record each time:

git cherry-pick <commitID>

// 增加 -x 参数,表示保留原提交的作者信息进行提交
git cherry-pick -x <commitID>

After executing cherry-pick, a new commit will be automatically generated for submission, and there will also be a new commit ID. Then you can git push to the remote branch to create a merge request. If it goes well, it can be merged normally. If you encounter a conflict, resolve the conflict.

Support batch cherry-pick

That is, you can set a start and end commit for a continuous time series of commits at a time, and perform a cherry-pick operation.

git cherry-pick commit1..commit100

// 可以看到,它的范围就是 commit1 到 commit100 之间所有的 commit,但是它这是一个(左开,右闭] 的区间,也就是说,它将不会包含 commit1 的 commit。

And if you want to include start-commit-id, you need to ^mark it , and it will become a [左闭,右闭] range of , and both commit1 and commit100 will be merged into the current branch.

git cherry-pick commit1^..commit100

Note: As mentioned above, the cherry-pick command will generate a new commit id every time a commit is picked. If we want to suspend the submission of each commit after selection, wait until all commits are selected and commit manually.

Then you can add -nthe option in front of the commitID, so that you can make a commit at the end .

git cherry-pick -n commit1^..commit100

Guess you like

Origin blog.csdn.net/qq_53931766/article/details/129591506