Git Cherry-Pick
Overview
Cherry-picking in Git is selecting specific commits from one branch and applying them to another. This is useful in scenarios where you want to merge only the changes of some specific commits into another branch (other than the branch where the commits reside). The purpose of cherry-picking is to provide the advantage of selectively moving specific commits rather than merging entire branches. This process of selecting specific commits is known as cherry-picking.
Prerequisite
To understand the concept of git cherry-picks, you should be familiar with commits in Git. Git Commit is the most commonly used command in Git and is used to create point-in-time snapshots of staged changes to Git repositories. This works like a save point. That means you can always go back to that point. Git checks before you change it, so it's considered a safe version of your project. Changes to be considered for commit should be in the staging area because staging allows you to save for preview before committing. If you want to push these changes to your git repository, you can use the git commit command to record these changes.
Commits help track the history and changes in your project repository. It can be useful if you want to come back later, find an error, or want to make some changes.
Each commit has a unique commit ID automatically generated at commit time. A commit ID is an encrypted number generated using a secure hashing algorithm called SHA.
The command used to commit in Git is:
Now you can use the commit command's -m option to write a commit message to the command line. Commits should always include a message. This provides an overview and description of the changes committed, making it easy for other users to see when and what was changed.
What is Git Cherry Pick?
Git cherry pick is selecting a few specific commits from a branch and merging only the selected ones into another. You can merge only some commits instead of the whole branch. Cherry-picking is similar to the concept of merging in Git, but unlike merging, you don't need to cherry-pick and merge entire branches here.
The primary use of cherry pick is to introduce the changes of a specific commit into another branch.
The following diagram illustrates the concept of cherry-picking in Git.
Let's understand this better with an example. Suppose you are working with multiple developers on a project, and each developer is working on an independent feature in their local branches. Now, you want to apply changes from your friend's feature branch to your main project but remember one thing. You want to merge only some of the changes. Merging branches completely doesn't solve your use case. Managing changes across multiple branches can be complex, and you want to avoid merging an entire branch into another branch. You must only pick one or two specific commits and merge them in another branch. This is where we see the need for cherry-picking. Selecting changes from other branches to the main project branch is called cherry picking. To apply cherry-picking, ensure you're on the branch you want to commit to. And then you can use the following command:
Here, a commit ID is a unique encrypted number generated using a secure hashing algorithm and can be retrieved using the git log command to get the history of commits within a git repository. You can use the commit ID to select which commit to use.
When to Use Cherry-pick
The usage of cherry pick comes when you have made a commit to the wrong branch by mistake or if you want the feature or the effect associated with some particular commits. Now you want to pick that commit from the current branch to some new branch. Then you can pick them up using the commit id and merge them to the new branch.
Or imagine a scenario where you are working on a project and another team member suggests some changes be included in the project, and those changes were implemented by someone in one of the commits on another branch. Then, you can cherry-pick commits to your project's local branch. Other uses of the cherry-pick are:
- Used when you need undoing and restoration of commits.
- Especially if a branch-wide merge is not possible or necessary in our case, we can consider the possibility of cherry-picking.
- It will help us to apply the changes from another branch without changing the branch itself.
- It is useful in error and troubleshooting scenarios.
- It is useful in a distributed work environment as it offers collaboration possibilities.
Some Scenarios in Which you can Cherry-pick
Let's see some scenarios in which we can apply the concept of cherry-picking in git:
Case 1: Making a commit in the wrong branch.
Suppose there are two local branches in our repository. We can list down the branches using the command:
The above command will show all the local branches in a repository, along with the * symbol marked in front of the name of the branch we are currently on.
Suppose, in our case, we have one main branch and one other branch named local_branch; we can check this by using the above command; the output will be:
The * symbol in front of the main means that presently, we are on the main branch, and to switch between different branches of the repository, we can use the command:
Here, we can provide the name of the branch we want to checkout.
To checkout to local_branch, we can write:
Now, the output of the command git branch would be:
Let's suppose we have a file named hello_world.cpp in our local_branch; let's add the file to the staging area using the:
Now, let's commit the staged changes using the commit command:
At this point, we noticed that we wanted to commit this change on the main branch and not here in the local_branch. In this situation, we don't prefer to merge the complete branch since there are other changes also in the local_branch which we don't want to include in the main branch, so we will pick these specific commits and will merge into the main branch, this is known as cherry picking.
To get the commit id, we will list the history of commits in the repository using the following:
This command lets you get the history of commits and details such as commit id, commit message, timestamp, author name, etc.
Then select the commit id of the commit you want to merge and switch to the main branch as you want the commit changes in the main branch. So for that, we will check out the following:
You can verify that you are on the main branch with the following:
The output looks like this:
Then select the commit of your choice with the following command:
You can then verify that this commit is in the main branch by listing the commits with the git log command.
Case 2: I want to add features or changes from another branch to the branch where the main project is.
Suppose that you are working on a project and another team member proposes some changes/features to include in your project, and that change has been implemented by someone in one of the commits in another branch. You can cherry-pick that commit to our project's local branch.
Let's say our branch is named main_project_branch, and the branch containing the feature is named dummy_branch; now we can get the commit id of that commit by switching to this dummy_branch and checking the logs of commits to do so we will write:
Now, we are on the dummy_branch. To get the commits, we can type:
From here, you can get the commit ID of the commit that contains your changes. Copy the commit ID and keep it for safe use. Then switch to main_project_branch.
We will cherry-pick the commit we picked using the:
Difference Between Cherry Picking and Merging in Git
Cherry Picking | Merging |
---|---|
Git cherry-pick applies the changes from specific commits from one branch to another. | Merging applies all commits from one branch to another or merges the complete branches. |
Because cherry-picking copies commits, the connection to the original structure is lost. | Merging preserves the original structure. |
Cherry picking is rarely used when we want to pick a couple of commits. | Merging is widely used and is a common part of the Git workflow. |
For cherry-picking, we switch to the branch where we want to apply the change, and then we use the command git cherry-picks <commit-id> to apply the changes of a particular commitment to the current branch. | In merging after switching to the branch in which we want to merge, we use the command git merge <name_of_branch, here instead of <name_of_branch, we will write the name of the branch that we want to merge to the current branch. |
Git Cherry Pick Example
Let's create a sample project to understand the need for cherry-picking.
We will create a folder named sample_project using the following:
Now, let's change the current directory to this above-created one using the following:
Let's now initialize an empty git repository by using the command:
Now, let's create some files in this folder by using the touch command:
You can also put data in this file. After doing this, let's add the changes to the staging area using the git add command:
Now, we will commit the staged changes to the git repository by:
We will perform the same steps for another file in this repository named file2 and commit its changes.
We should have two commits, one for file1 and the other for file2; we can confirm this using the git log command.
Now, let's say we want the changes of file1 in another branch (named developer_branch), but we can't directly merge the branches, as merging also includes the changes of file2, but we only want file1, so for that, we will cherry pick commit1, as in that version only the changes for file1 were there.
To get the commit id, we will use git log and pick the commit id of commit 1.
Now, we will switch to the developer_branch using the following:
And now, we can cherry-pick the commit here using the command:
Where commit_id is the id of commit 1, which contains only file one changes.
Conclusion
- Git cherry-pick It is the concept of selecting a specific commit from one branch and applying it to another.
- Git commits create point-in-time snapshots of staged changes to Git repositories.
- Merge combines all commits from one branch into the current branch.
- Cherry-picking is useful in bug-fix scenarios.
- Cherry-picking works if you accidentally or mistakenly commit to the wrong branch or if you want to merge a specific commit containing a function into another branch.
- git cherry-pick <commit-id> is the command to cherry-pick the commit to the current branch.
- Commit-id can be retrieved by using the git log command, which details the history of commits along with their unique IDs.
- Compared to git cherry-pick, merging is a more general and easy-to-use concept in Git.
- Git cherry-pick also useful for team collaboration purposes.
- Excessive use of cherry-picking should be avoided as duplicate commits can clutter your repository, and you can also lose the ability to track the history of commits in your repository.