Git Cherry-Pick Code Changes

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to manage their codebase efficiently. One of the most useful features of Git is the ability to cherry-pick changes from one or more commits. This allows developers to apply specific changes to their codebase without having to merge entire branches.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12744{{"`Git Cherry-Pick Code Changes`"}} end

Git Cherry-Pick

As a developer, you are working on a project with multiple branches. You have identified a specific change that was made in a previous commit that you would like to apply to your current branch. However, you do not want to merge the entire branch as it contains other changes that you do not need. In this scenario, you can use the git cherry-pick command to apply the specific change to your current branch.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground. Follow the steps below to complete the challenge:

  1. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/labex-labs/git-playground
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Create and switch to a branch called one-branch, create a file called hello.txt, write "hello,world" in it, add it to the staging area and commit it with the message "add hello.txt":
git checkout -b one-branch
echo "hello,world" > hello.txt
git add .
git commit -m "add hello.txt"
  1. Identify the hash of the commit created in the previous step to apply to the master branch:
git log
  1. Checkout the master branch and apply the change to the master branch:
git checkout master
git cherry-pick 1609c283ec86ee4
  1. Verify that the change has been applied to the master branch:
git log

This is the result of running git log on the master branch:

commit e2f3c6af9570f4eac2580dea93ca8133f1547d53 (HEAD -> master)
Author: xiaoshengyunan <@users.noreply.github.com>
Date:   Sat Jul 15 14:30:31 2023 +0800

    add hello.txt

commit d22f46ba8c2d4e07d773c5126e9c803933eb5898 (origin/master, origin/HEAD)
Author: Hang <huhuhang@users.noreply.github.com>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file2.txt

commit cf80005e40a3c661eb212fcea5fad06f8283f08f
Author: Hang <huhuhang@users.noreply.github.com>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt

commit b00b9374a7c549d1af111aa777fdcc868d8a2a01
Author: Hang <huhuhang@gmail.com>
Date:   Wed Apr 26 14:16:00 2023 +0800

    Initial commit

Summary

In this lab, you learned how to use the git cherry-pick command to apply specific changes from one or more commits to your current branch. This is a powerful feature of Git that allows developers to manage their codebase efficiently and avoid merging entire branches.

Other Git Tutorials you may like