Git

Is It Possible to Cherry-pick a Commit From Another Git Repository?

β€œ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:

cd "C:\Git\ReposB"

Step 2: Check Git Log

Then, check the commit history of the working repository and view the position of HEAD:

$ git log --oneline

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:

$ git remote add origin https://github.com/laibayounas/demo.git

Here, the remote origin has been added:

Step 4: Fetch Remote Content

Now, fetch the content of the remote repository into the local directory:

$ git fetch origin

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:

$ git log origin/main --oneline

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:

$ git cherry-pick deaeaf7

Step 7: Verify Changes

Lastly, check the Git log of the local directory to verify the new changes:

$ git log --oneline

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.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.