DEV Community

Harsh Mange
Harsh Mange

Posted on • Originally published at harshmange.hashnode.dev on

git cherry-pick 101: A beginner's guide to selectively applying commits

What the hack is git cherry-pick?

Git Cherry-pick is a powerful tool that allows developers to selectively apply specific commits from one branch to another. This can be useful in a variety of situations, such as when a bug fix needs to be applied to a release branch, or when a feature needs to be ported from a development branch to a production branch. In this article, we will explore the basic usage of git cherry-pick and provide some examples of how it can be used to streamline your workflow.

Scenario

To start, let's assume that you have a repository with two branches: "master" and "feature". The "master" branch is your main branch, and the "feature" branch is where you are working on a new feature. You've been working on this feature for a while and have made a number of commits to the "feature" branch. However, you now realize that you need to apply one of those commits to the "master" branch.

To do this, you can use the git cherry-pick command. The basic syntax of this command is as follows:

Syntax

git cherry-pick <commit-hash>

Enter fullscreen mode Exit fullscreen mode

Explanation

Where "commit-hash" is the unique hash that identifies the commit you want to apply. You can find the commit hash by running the command "git log" in the terminal.

Once you've identified the commit hash, you can switch to the "master" branch and run the git cherry-pick command. This will apply the specified commit to the "master" branch. You can now push the changes to the remote repository, and the commit will be included in the "master" branch.

Real-world example

$ git checkout feature
$ git log
commit 789abcdef (HEAD -> feature)
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 22:00:00 2023

Fixed a bug in the login system

commit 345defghi
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 21:00:00 2023

Implemented a new feature

commit 123abcdef
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 20:00:00 2023

Initial commit

$ git cherry-pick 789abcdef
$ git log
commit 789abcdef (HEAD -> feature)
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 22:00:00 2023

Fixed a bug in the login system

commit 345defghi
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 21:00:00 2023

Implemented a new feature

commit 123abcdef
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 20:00:00 2023

Initial commit
$ git checkout master
$ git cherry-pick 789abcdef
$ git log
commit 789abcdef (HEAD -> master)
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 22:00:00 2023

Fixed a bug in the login system

commit 654abcdef
Author: Harsh <harsh@example.com>
Date: Mon Jan 25 19:00:00 2023

some other changes

commit 123abcdef
Author: Harsh <joh

Enter fullscreen mode Exit fullscreen mode

More about me: https://harshmange.netlify.app

Let's connect on GitHub and LinkedIn !!

git commit -m "mastered git cherry-pick"

Top comments (0)