JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Git cherry pick command usage

Author:JIYIK Last Updated:2025/04/05 Views:

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to another. git cherry-pick can be used to undo changes. For example, suppose you accidentally committed to the wrong branch. We can switch to the correct branch and "pick" the commit to where it should belong.

When to use git cherry pick

git cherry-pick is a very useful tool, but we will find that it is not always optimal in practice. Cherry picking can lead to duplicate commits in many cases, so traditional merges are preferred. That being said, git cherry-pick is a handy tool for some scenarios...

Teamwork

Many times, teams will find that individual members are using or working around the same code. Perhaps a new product feature has a backend and frontend component. There may be some shared code between the two product departments. Perhaps a backend developer created a data structure that the frontend also needs to use. A frontend developer can use git cherry-pick to select the commit that created this hypothetical data structure. This selection will enable the frontend developer to continue subsequent development on their project.

Bug Fixes

When a bug is discovered, it is very important to get a fix to end users as quickly as possible. For some example scenarios, assume that a developer has started working on a new feature. During the development of the new feature, they identify a pre-existing bug. The developer creates an explicit commit to patch this bug. This new patch commit can be "picked" directly to the master branch to fix the bug before it affects more users.

Undo changes and restore lost commits

Sometimes a feature branch may become out of date and not be merged into the master branch. Sometimes a pull request may be closed without being merged. Git never loses these commits, and commands such as git log and git reflog make it possible to find them and recover them.

Using git cherry pick

After saying so much, it's finally time for the protagonist to appear. Let's take a look at how to use this command.

To demonstrate how to use git cherry-pick, let's assume we have a repository with the following branch state:

a - b - c - d   Main
         \
           e - f - g Feature

The usage of git cherry-pick is very simple and can be executed as follows:

$ git cherry-pick commitSha

In this case, commitShais a commit reference. We can find that commit reference using git log . In this example, let's say we want to use a commit in mainf . First, we make sure we are working on the main branch.

$ git checkout main

Then we execute cherry-pick using the following command:

$ git cherry-pick f

After execution, our Git history will look like this:

a - b - c - d - f   Main
         \
           e - f - g Feature

fThe commit has been successfully "picked" to the main branch

git cherry pick command options

git cherry pick can also be followed by some execution options.

-edit

-editThe -p option will prompt git to commit the message before applying the cherry-pick operation.

--no-commit

The -p option with -p --no-commitwill perform a cherry-pick that will not make a new commit, but instead move the contents of the target commit into the working directory of the current branch.

--signoff

--signoffThe option will add a "signoff" signature line to the end of the cherry-pick commit message.

In addition to these useful options, git cherry-pick also accepts various merge strategy options.

Additionally, git cherry-pick accepts options for merge conflict resolution, including options: --abort --continueand --quit. These options are covered in more depth in git merge and git rebase .

Cherry picking is a convenient and powerful command that is very useful in some cases. However, we should not abuse cherry pick to replace git merge or git rebase. The git log command is needed to help cherry pick find commits.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Git Stash and Shelve in IntelliJ IDEA

Publish Date:2025/04/04 Views:113 Category:Git

git stash This article will distinguish between and when using IntelliJ IDEA git shelve . These two come into play when we want to switch between multiple tasks while working and return to them later. IntelliJ IDEA allows us to work on diff

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial