βgit cherry-pickβ is a useful command in Git that allows developers to pick the commit from one branch and apply it to another Git branch or repository. Sometimes, while working on Git, developers commit to the wrong branch that they need to copy to another branch. For this purpose, you can navigate to the correct branch and cherry-pick the desired commits.
This blog will discuss the method of cherry-picking a commit from another Git repository.
How to Cherry-pick a Commit From Another Git Repository?
It is allowed to cherry-pick single or multiple commits from other Git repositories. To do so, switch to the particular local directory. Then, add the particular GitHub repository as a remote and fetch its content. After that, check the Git log and select the desired commit. Next, cherry-pick that commit by executing the βgit cherry-pick <commit-id>β command.
Step 1: Go to Desired Repository
First, write out the below-listed command and switch to the particular directory:
Step 2: Check Git Log
Then, check the commit history of the working repository and view the position of HEAD:
In the below output, it can be observed that the HEAD is pointing to the β72889b1β commit hash:
Step 3: Add Remote URL
Next, add the remote URL of the desired remote repository using the given-provided command:
Here, the remote origin has been added:
Step 4: Fetch Remote Content
Now, fetch the content of the remote repository into the local directory:
Step 5: View Git Log of Remote Repository
View the list of its commits by running the βgit logβ command along with the remote repository name:
The below-screenshot displays the commit history including all commits. Choose the desired commit id for cherry-picking. For instance, we have selected the βdeaeaf7β commit hash:
Step 6: Cherry-pick Desired Commit
Next, execute the βgit cherry-pickβ command and specify the particular commit id of the remote repository:
Step 7: Verify Changes
Lastly, check the Git log of the local directory to verify the new changes:
According to the given image, the cherry-pick operation has been performed successfully:
We have efficiently explained the method of cherry-picking the commit from another Git repository.
Conclusion
Git allows users to cherry-pick single or multiple commits from another Git directory. Users can cherry-pick one or multiple commits from the remote repository. For this purpose, first, navigate to the local repository. Then, add the remote URL of a particular remote directory and fetch its content. Next, view the commit history of the remote repository and choose the desired commit. Lastly, run the βgit cherry-pick <commit-id>β command to cherry-pick a specific commit. This write-up demonstrated the procedure of cherry-picking the commit from another Git repository.