0
0
Gitdevops~30 mins

git cherry-pick a single commit - Mini Project: Build & Apply

Choose your learning style9 modes available
git cherry-pick a single commit
📖 Scenario: You are working on a project with two branches: main and feature. A useful commit was made on the feature branch, and you want to copy that exact commit into the main branch without merging all changes.
🎯 Goal: Learn how to use git cherry-pick to copy a single commit from one branch to another.
📋 What You'll Learn
Create a feature branch from main
Make a commit on feature branch with exact message and content
Switch back to main branch
Use git cherry-pick with the commit hash from feature branch
Verify the commit is applied on main branch
💡 Why This Matters
🌍 Real World
Cherry-picking is useful when you want to copy specific fixes or features from one branch to another without merging all changes. For example, applying a bug fix from a feature branch to the main production branch.
💼 Career
Understanding cherry-pick helps in managing code changes efficiently in team projects and production environments, a common task for developers and DevOps engineers.
Progress0 / 4 steps
1
Create a feature branch and make a commit
Create a new branch called feature from main using git checkout -b feature. Then create a file named feature.txt with the content Feature work. Stage and commit this file with the exact commit message Add feature.txt file.
Git
Need a hint?

Use git checkout -b feature to create and switch to the new branch. Use echo to create the file, then git add and git commit to save changes.

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

Use git checkout main to switch branches.

3
Find the commit hash on feature branch
Use git log feature --oneline -1 to find the latest commit hash on the feature branch. Assign this hash to a variable named commit_hash in your shell.
Git
Need a hint?

Use git log feature --oneline -1 to get the latest commit on feature. Use shell command substitution to assign the hash.

4
Cherry-pick the commit onto main
Use git cherry-pick $commit_hash to apply the commit from feature branch onto main. Then use git log -1 --oneline to print the latest commit message on main branch.
Git
Need a hint?

Use git cherry-pick $commit_hash to copy the commit. Then git log -1 --oneline shows the latest commit message.