top of page

Cherry-Picking Commits

Objective: The objective of this assignment is to learn how to cherry-pick commits in Git. Cherry-picking allows you to select and apply specific commits from one branch to another, providing a way to incorporate changes selectively.


 You need to selectively apply specific commits from one branch to another without merging the entire branch.


Instructions:


Part 1: Repository Setup 


1. Task 1: Create a new directory on your computer for this assignment and name it "GitCherryPickAssignment."


2. Task 2: Navigate to the "GitCherryPickAssignment" directory in your terminal or command prompt.


3. Task 3: Initialize a new Git repository in this directory.


   

   git init

   


4. Task 4: Create a new text file named "README.md" in the repository.


   

   touch README.md

   


Part 2: Making Commits 


1. Task 1: Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.


   

   nano README.md  # Use your preferred text editor

   


2. Task 2: Add some content to the "README.md" file. For example:


   markdown

   # Git Cherry-Pick Assignment


   This is the main README file.

   


3. Task 3: Save the changes to the "README.md" file and exit the text editor.


4. Task 4: Commit the changes to the repository.


   

   git add README.md

   git commit -m "Initial commit: Add README"

   


Part 3: Creating a Feature Branch (10 points)


1. Task 1: Create a new branch named "feature/branch-A."


   

   git branch feature/branch-A

   


2. Task 2: Switch to the new branch.


   

   git checkout feature/branch-A

   


3. Task 3: Open the "README.md" file again and add some content specific to branch A.


   

   nano README.md

   


4. Task 4: Save the changes and exit the text editor.


5. Task 5: Commit the changes in "feature/branch-A."


   

   git add README.md

   git commit -m "Add branch-A specific content to README"

   


Part 4: Cherry-Picking a Commit 


1. Task 1: Switch back to the "main" branch.


   

   git checkout main

   


2. Task 2: Create a new branch named "feature/branch-B."


   

   git branch feature/branch-B

   


3. Task 3: Switch to the new branch.


   

   git checkout feature/branch-B

   


4. Task 4: Cherry-pick the commit from "feature/branch-A" that adds branch-A specific content to the README.


   

   git cherry-pick <commit-hash>

   


   Replace `<commit-hash>` with the actual hash of the commit you want to cherry-pick from "feature/branch-A."


Solutions

Part 1: Repository Setup



# Task 1

mkdir GitCherryPickAssignment


# Task 2

cd GitCherryPickAssignment


# Task 3

git init


# Task 4

touch README.md



Part 2: Making


 Commits



# Task 1

nano README.md  # Use your preferred text editor


# Task 2

# Add content to README.md and save


# Task 4

git add README.md

git commit -m "Initial commit: Add README"



Part 3: Creating a Feature Branch



# Task 1

git branch feature/branch-A


# Task 2

git checkout feature/branch-A


# Task 3

nano README.md


# Task 4

# Add content specific to branch A and save


# Task 5

git add README.md

git commit -m "Add branch-A specific content to README"



Part 4: Cherry-Picking a Commit



# Task 1

git checkout main


# Task 2

git branch feature/branch-B


# Task 3

git checkout feature/branch-B


# Task 4

git cherry-pick <commit-hash>  # Replace <commit-hash> with the actual hash


bottom of page