Is it possible to cherry-pick a commit from another git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In software development, dealing with multiple git repositories is commonplace. There might be occasions where a particular commit from one repository (Repo A) is needed in another repository (Repo B). Git, a powerful version control system, allows for such operations through a multitude of commands and operations, including the ability to cherry-pick a commit from a different repository.
Understanding Cherry-Picking in Git
Cherry-picking in git is the process of selecting a specific commit from one branch and applying it onto another. This can be extremely useful in situations where only certain changes from a branch need to be included in another line of development.
How to Cherry-Pick a Commit from Another Repository
To cherry-pick a commit from another repository, follow the steps outlined below:
Step 1: Add the External Repository as a Remote
First, you need to add the external repository from which you want to cherry-pick the commit as a remote to your current repository. This is done using the git remote add command.
By executing this command, you create a local reference to the other repository, which allows you to fetch its data.
Step 2: Fetch the Commit from the External Repository
Once the remote is added, you need to fetch the branches and commits from that repository.
This command downloads objects and refs from the other repository. You won't merge anything yet; you're just bringing the commits into your local repository environment.
Step 3: Cherry-Pick the Commit
Now that you have access to the commits from the remote repository, you can cherry-pick the commit you need:
Replace <commit-hash> with the actual commit hash from the external repository that you want to cherry-pick. This will start the cherry-picking process, applying that commit onto your current branch.
Step 4: Handle Merge Conflicts
If the cherry-picked changes conflict with the current branch’s content, git will pause the operation and prompt you to resolve conflicts. After resolving any conflicts:
These commands add the resolved files and continue with the application of the commit.
Step 5: Push Changes (if necessary)
If you need to share this change with a shared repository:
Considerations and Best Practices
- Repository Integrity: Ensure that cherry-picking is permissible in the context of your project’s workflow, as it can introduce changes without the context provided by the source branch’s own history.
- Commit Compatibility: Check if the commit applies cleanly to your branch. Structural changes in one branch might not be present in another, leading to conflicts or unexpected issues.
- Licensing and Codebase Policies: Be aware of licensing issues when cherry-picking between repositories, particularly from public to private repositories.
Summary Table
| Step | Command | Purpose |
| 1 | git remote add other-repo <url-to-other-repo> | Add another repository as a remote. |
| 2 | git fetch other-repo | Fetch commits and branches from the newly added remote. |
| 3 | git cherry-pick <commit-hash> | Apply a specific commit from the fetched data to your current branch. |
| 4 | git add .
git cherry-pick --continue | Resolve conflicts and continue cherry-picking. |
| 5 | git push origin <branch-name> | Push the changes to a remote repository if necessary. |
Conclusion
Cherry-picking commits from another repository allows developers to selectively apply changes, ensuring that only the necessary alterations are integrated into a project. This guide provides a comprehensive approach to effectively leverage git's capabilities in handling commits across different repositories.

