Git

Can You cherry-pick Multiple Commits?

In Git, developers may need to include specific changes or modifications to their current branch. For this purpose, the “$ cherry-pick” command is useful. It takes changes from a particular commit and applies them to the current working branch in a new commit. However, Git cherry-pick does not alter the history of the current branch; instead, it adds commits.

This blog illustrates the method to cherry-pick multiple commits.

Can You cherry-pick Multiple Commits?

Yes, the developers can utilize the “cherry-pick” command for multiple commits by utilizing the terminal. To cherry-pick multiple commits, first, create a new repository and move to it. Next, create and add a new file. Commit the changes and update the repository. Then, remove all files using the “$ rm* <file-name>” command. Check the reference log history and finally, execute the “$ git cherry-pick <commit-ref>..<commit-ref>” command to cherry-pick multiple commits.

Now, let’s move toward the implementation of the above-discussed scenario!

Step 1: Create New Directory
First, create a new repository by utilizing the “mkdir” command:

$ mkdir cherry-pick

Step 2: Navigate to Directory
Next, run the “cd” command to move to the newly created directory:

$ cd cherry-pick

Step 3: Create and Modify File
Execute the below-provided command to create and update the file simultaneously:

$ echo "first file">file1.txt

Step 4: Track File
Execute the following command to track a file to the staging area:

$ git add .

Step 5: Commit Changes
Update and save the added changes in the repository by utilizing the following command:

$ git commit -m "first commit"

Step 6: Remove File
Now, execute the “$ rm*” command to remove all files from the directory:

$ rm *.txt

The above command will delete all the text files:

Step 7: Update Repository
Now, update and save the changes to the Git directory:

$ git add .

Step 8: Commit Changes
Commit changes by utilizing the “git commit” command along with the required message:

$ git commit -m "second commit"

Step 9: Create and Update File
Create a new file or update the existing one with the help of the given command:

$ echo "second file">file2.txt

Step 10: Add file
Next, track the file to the staging area by specifying the file name in the given command:

$ git add file2.txt

Step 11: Commit Changes
Execute the provided command to commit the added changes into the repository:

$ git commit -m "third commit"

Step 12: Check Reference Log History
To view the log reference history of a specific branch, mention its name in the “git reflog” command:

$ git reflog master

Step 13: cherry-pick Multiple Commits
Finally, execute the “git cherry-pick” command along with commit references:

$ git cherry-pick c119ea2..ee47790

As you can see in the below-provided output, multiple commits are cherry picked successfully:

Step 14: Check Log Reference
Lastly, run the “git reflog .” command for checking the log reference:

$ git reflog .

It can be observed that multiple commits are cherry picked and appended successfully to the current HEAD:

We have offered the simplest method for cherry picking multiple commits in Git.

Conclusion

Yes, you can cherry-pick multiple commits by utilizing the terminal. To cherry-pick multiple commits, first, create a new repository and move to it. Then, create and add a new file. Commit the changes and update the repository. Next, remove the added file using the “$ rm* <file-name>” command. Check the reference log history and finally, execute the “$ git cherry-pick <commit-ref>..<commit-ref>” command to cherry-pick multiple commits. In this blog, we have demonstrated the method to cherry-pick multiple commits.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.