DEV Community

Cover image for How to Cherry-Pick a Single Commit from One Branch to Another in Git
Anusha Kuppili
Anusha Kuppili

Posted on

How to Cherry-Pick a Single Commit from One Branch to Another in Git

Have you ever been working on a feature branch and realized that you want just one specific commit merged into your master branch — without merging all the other ongoing work? Git’s cherry-pick command is your best friend in such cases!

In this post, I’ll walk you step-by-step through how to cherry-pick a specific commit from the feature branch and apply it onto master. This is super useful when you want to move one change only without bringing in unfinished work.


/usr/src/kodekloudrepos
Enter fullscreen mode Exit fullscreen mode

There are two branches:

  • master
  • feature (where ongoing work is happening)

There’s a commit on feature with the message Update info.txt that you want to bring into master.


Step 1: Navigate to Your Repository

Make sure you are inside the cloned Git repo:

cd /usr/src/kodekloudrepos
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to the master Branch

Checkout the branch where you want to apply the commit:

git checkout master
Enter fullscreen mode Exit fullscreen mode

Step 3: Find the Commit Hash on feature Branch

View the commit history on the feature branch to identify the hash of the commit with the message Update info.txt:

git log feature
Enter fullscreen mode Exit fullscreen mode

Look for the commit hash (a long alphanumeric string) next to the commit message you want.


Step 4: Cherry-pick the Commit

Apply the commit to your current branch (master) by running:

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Replace <commit-hash> with the actual commit ID you found.

Git will apply the changes from that single commit to your master branch, creating a new commit there.


Step 5: Push Your Changes

Push the updated master branch to the remote repository:

git push origin master

Enter fullscreen mode Exit fullscreen mode


Conclusion

Git cherry-pick is a powerful command that lets you select individual commits and move them across branches without merging everything. This control is especially helpful when coordinating teamwork and managing unfinished features.

If you found this guide helpful or have questions, feel free to leave a comment!

Happy coding! 🚀


References

If you'd like video tutorials related to this topic, check out my Git tutorials on YouTube.

Top comments (0)