How to Cherry-Pick a Commit in Git

Abdul Jabbar Feb 02, 2024
  1. When to Use the Git cherry-pick?
  2. Git cherry-pick Command
  3. Restore Deleted Commits & Undoing Changes in Git
How to Cherry-Pick a Commit in Git

cherry-pick is a very useful command that is amazing and very helpful in various scenarios while using Git. In Git, the cherry-pick command allows us to combine sorted and separated commits from one branch into the current HEAD branch.

We can explain as picking a commit from the particular branch and transferring it to another branch. This command can be useful for undoing changes from that branch. For example, if any commit is accidentally pushed to the undesired branch. We can switch to the desired branch and cherry-pick that accidentally commit to the desired branch where it should be.

When to Use the Git cherry-pick?

Git cherry-pick is a powerful tool but can’t always be the best choice. We should apply cherry-pick not common because it easily makes duplicate commits when we combine a commit into our HEAD branch using this command.

Git is responsible for making a new commit with the same requirements as the last commit. Git cherry-pick should be used in a scenario when we have mistakenly committed the new changes on the wrong branch and we pushed it to the remote repository.

After pushing it to the remote repository, we realize that the pushed commit was not required to push on a particular branch, so we have to use the Git cherry-pick command to pick that commit by unique ID and transfer it to the correct branch.

Whenever we get a chance to apply traditional Merge or Rebase to combine, we should consider cherry-picking because cherry-pick must be reserved for rare cases where the merge or rebase is not applicable.

Git cherry-pick Command

We only need to specify the commit we want to combine into our current HEAD branch from the commit we did on the wrong branch, et12x8iu is the commit ID we need to specify to combine it on the current branch.

Below is the command which will transfer the wrong commit to the new latest commit.

$ git cherry-pick et12x8iu 

In this way, we will directly commit specified revisions to our currently checked-out branch.

Suppose we further need some modification in our work. In that case, we can also use the Git command to only include the commit’s changes to our main working copy without directly committing them. This way, it will directly come into a modified working copy, and we can modify the files as per our requirements.

Following is the command to get the wrong commit into our working directory:

$ git cherry-pick et12x8iu --no-commit

Restore Deleted Commits & Undoing Changes in Git

Sometimes a feature branch has some complications and does not get combined into a current branch, and sometimes a pull request might stop without merging due to different working requirements.

Git’s special feature is that it never loses track of those commits, and through some commands like git log and git reflog, they can be tracked again and cherry-picked back to their place where they should be in the first place.

We can say the option cherry-pick should not be used instead of git merge or git-rebase. The git log command can be applied to help to sort out commits to cherry-pick in the particular branch.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Commit