JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Cherry-Pick Merge Commits in Git

Author:JIYIK Last Updated:2025/03/29 Views:

When multiple developers from the same team are working on a project, regardless of the complexity of the project, handling and managing changes between Git branches becomes very difficult.

Sometimes, we need to merge some specific commits into another branch instead of merging the entire branch, and for this, we use a fundamental tool in Git known as cherry picking.

Cherry picking is a great tool when it comes to selecting just one commit from the entire list of commits in a repository branch.


git cherry-pickCommands in Git

git cherry-pickis an influential command that allows moving random Git commits from a branch in the repository and adding them to the repository you are currently working on HEAD. This command also helps in undoing changes related to past developments of your team.

For example, if we accidentally made a commit to the wrong branch, we will move to the correct branch and cherry-pick the commit into the correct location in the new branch in the repository.

When using git mergeor git rebase, all commits from one branch are mixed. While cherry-pickthe command enables us to select selected commits to assimilate.


Cherry-Pick Merge Commits in Git

Cherry-picking a merge commit is not as easy as it seems. When we perform the merge process, we work with two branches of the same repository at the same time.

For example, use the Git command to parent-Bmerge branch into parent-Abranch.

So, when we want to cherry-pick a specific commit, i.e., merge with two parent branches, we need to -mtell the -p option to cherry-pickthe -p command which commit it should use diffagainst. We will use -p in the following command commit hashto mention the specific parent branch.

The syntax for the above case is explained below.

git cherry-pick -m <commit hash>

Let us explain this situation with the following example of the above command.

M - N - O - P - Q (branch A)
     \     /
      R - S - T (branch B)

As shown above, Pit is a merge commit. If we run git cherry-pickthe command, Git will pass a warning about it.

So now we have to mention the parent branch number with the commit id. It will be executed in the following command.

git cherry-pick -m 1 P

This command will get all commits cherry-pickfrom the above example .O- P

We can also use the following command to perform the above operations.

git cherry-pick -m 2 P

This will R- S- Pselect all commits from in the above example.

It destroys all the changes we made in the parent and that we didn't define in that commit -m.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial