乐闻世界logo
搜索文章和话题

How to cherry-pick a range of commits and merge them into another branch?

1个答案

1

In Git, selecting a series of commits and merging them into another branch is a common task, especially in collaborative projects. This can be achieved through several methods, but one of the most commonly used approaches is the git cherry-pick command. Next, I will detail how to use this command, along with providing a specific example to illustrate the entire process.

Step 1: Identify the commits to cherry-pick

First, identify the specific commits you want to cherry-pick and merge into another branch. This can be done by examining the commit history, for instance, with the command:

bash
git log --oneline

This will display a brief summary and hash value for each commit. Note down the hash values of the commits you want to cherry-pick.

Step 2: Checkout the target branch

Before cherry-picking commits, switch to the branch where you want these commits to be merged. Use the command:

bash
git checkout target-branch

where target-branch is the name of your target branch.

Step 3: Cherry-pick commits

Now, you can begin cherry-picking the commits identified in Step 1. For a single commit, use:

bash
git cherry-pick <commit-hash>

For a series of consecutive commits, use:

bash
git cherry-pick <start-commit-hash>^..<end-commit-hash>

Note: The ^ symbol is used to include the starting commit. If the commits are not consecutive, you can run multiple git cherry-pick commands sequentially, each with a different commit hash.

Step 4: Handle conflicts

During cherry-picking, conflicts may occur. Git will pause the process, allowing you to manually resolve conflicts. After resolving conflicts, use the command below to proceed:

bash
git cherry-pick --continue

If you choose not to proceed with this cherry-pick, use:

bash
git cherry-pick --abort

Example

Assume you are working on the develop branch and need to cherry-pick certain commits into the feature-branch. The commit hashes are a1b2c3d and d4e5f6g.

  1. Checkout the target branch:
bash
git checkout feature-branch
  1. Cherry-pick the commits:
bash
git cherry-pick a1b2c3d d4e5f6g

This approach allows you to efficiently cherry-pick specific commits from one branch into another, keeping your project organized and clear.

2024年6月29日 12:07 回复

你的答案