0
0
Gitdevops~15 mins

Why cherry-pick is useful in Git - See It in Action

Choose your learning style9 modes available
Why cherry-pick is useful
📖 Scenario: You are working on a software project with multiple branches. Sometimes, you want to take a specific change from one branch and apply it to another without merging all changes. This is where git cherry-pick helps.
🎯 Goal: Learn how to use git cherry-pick to copy a single commit from one branch to another.
📋 What You'll Learn
Create a new branch called feature from main
Make a commit on feature with a specific message
Switch back to main branch
Use git cherry-pick to apply the commit from feature to main
Show the commit log on main to confirm the cherry-pick
💡 Why This Matters
🌍 Real World
Cherry-pick is useful when you want to apply a specific fix or feature from one branch to another without merging all changes. For example, applying a bug fix from a development branch to the stable release branch.
💼 Career
Understanding cherry-pick helps you manage code changes efficiently in team projects, especially when working with multiple branches and releases.
Progress0 / 4 steps
1
Create a new branch and make a commit
Create a new branch called feature from main and switch to it. Then create a file named feature.txt with the content New feature added and commit it with the message Add new feature.
Git
Need a hint?

Use git checkout -b feature main to create and switch to the new branch.

Use echo to create the file and git commit -m to commit.

2
Switch back to main branch
Switch back to the main branch using the correct git checkout command.
Git
Need a hint?

Use git checkout main to switch branches.

3
Cherry-pick the commit from feature branch
Use git cherry-pick with the commit hash from the feature branch to apply that commit to the main branch. First, find the commit hash by running git log feature -1 --pretty=format:%H. Then use git cherry-pick <commit-hash>.
Git
Need a hint?

Use git log feature -1 --pretty=format:%H to get the latest commit hash from feature.

Then use git cherry-pick <commit-hash> to apply it.

4
Show the commit log on main branch
Run git log -1 --oneline on the main branch to display the latest commit message and confirm that the cherry-pick was successful.
Git
Need a hint?

Use git log -1 --oneline to see the latest commit message.