Git

How to cherry-pick a Range of Commits and Merge them into Another Branch?

While working on Git, developers often want to move the commits of one branch to another branch without merging the branches. However, it becomes hard to move each commit one by one. For this purpose, utilizing the “git cherry-pick” command is helpful. It allows developers to choose the range of commits from one branch and put them into their target branch.

This blog will explain the method of cherry-picking a range of commits in one branch and merging them into the target branch.

How to cherry-pick a Range of Commits and Merge them into Another Branch?

To cherry-pick a range of commits in one branch and combine them into the target branch, first, move to the local repository. Then, check the Git log of the working branch, and choose the range of commits. After that, copy the commit id of the starting and ending commits in the range. Next, switch to the target branch and run the “git cherry-pick <start-commit-id>…<end-commit-id>” command to merge old branch commits to the target branch.

Step 1: Switch to Required Directory

Redirect to the particular local directory with the help of the given-below command:

$ cd "C:\Git\RepoQ"

 
Step 2: View Git Log

Next, check the Git log to view the commit history of the current working branch:

$ git log --oneline

 
The below-screenshot displays commit in the “master” branch. Choose the range of commits and copy the SHA-hash of the first and last commit. For instance, we have selected the four commits and copied the commit hash of the “e2c5584” and “4367d2a” commit:


Step 3: Check Available Branches

Then, view the list of available branches in the current repository:

$ git branch

 
The below output indicates that the working repository contains two branches. Select the target branch and navigate to it. For instance, the “alpha” branch is our target branch:


Step 4: Switch to Target Branch

Now, run the given-below command with the target branch name and move to it:

$ git switch alpha

 

Step 5: Check Git Log

View the commit history to check the position of HEAD in the target branch:

$ git log --oneline

 
Here, it can be seen that the HEAD is pointing to the “192bf22” commit id:


Step 6: Cherry-pick Range of Commits

Next, run the “git cherry-pick” command and specify the range of commits that needs to be merged in the working branch:

$ git cherry-pick e2c5584...64d50af

 

Step 7: Verify Changes

Finally, view the commit history of the current branch to verify changes:

$ git log --oneline

 
According to the given-provided screenshot, the cherry-pick operation has been performed successfully, and the commits of the “master” branch have been merged into the “alpha” branch:


We have provided the procedure of cherry-picking a range of commits and merging them into another branch.

Conclusion

To cherry-pick a range of commits and combine them into another branch, first, redirect to the local directory and check its Git log. Then, select the range of commits and copy the commit hash of the starting and ending commits in that range. Lastly, move to the target branch and run the “git cherry-pick <start-commit-id>…<end-commit-id>” command. This blog explained the method of cherry-picking a range of commits and merging them into another branch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.