When working with multiple repositories, there are times when you want to bring in just one (or a few) commits from another project, without merging the entire repo. In Git, you can cherry-pick from another repo by treating it like a remote.
In this article, I’ll walk you through two steps with examples. Let’s jump into them.
- You want to reuse a bug fix or feature commit from another project.
- You don’t want to merge all changes, just specific commits.
- Keeps history clean by pulling only essential changes.
However, cherry-picking across repos should be used carefully, because it can lead to duplicate content and conflicts.
First of all, suppose your current repo is RepoA, and you want a commit from RepoB (on GitHub or Locally).
- Add the external repo as a remote
git remote add repoB https://github.com/username/RepoB.git
# or if the RepoB is local
git remote add repoB C:/Users/User/RepoB
repoB
is just a name (alias) for the external repository.
You can choose any name you like (e.g. external
, upstream2
, sourceRepo
).
If you already have a remote with that name, pick a different alias.
- Fetch from that remote
git fetch repoB
This will bring all branches and commits from RepoB, so that your local Git can see them. You can now reference branches like repoB/main
or repoB/feature-xyz
.
- Identify commit(s) you need
You need the commit hash (SHA) you want to cherry-pick.
List commits in a branch from the external remote:
git log repoB/main --oneline
# or
git log --graph --decorate repoB/main
Once you see the hash (say abc1234
), that’s the commit you want to pick.
- Cherry-pick the commit
Switch to the branch in RepoA where you want to apply the change:
git checkout my-feature-branch
Now cherry-pick:
git cherry-pick abc1234
# or cherry-pick a range
git cherry-pick startSHA..endSHA
# or multiple commits
git cherry-pick abc1234 def5678
This will apply the changes from commit abc1234
into your branch, creating a new commit (with the same content, possibly a different hash).
Then, resolve conflicts if any, or if you decide it’s too messy or want to abort:
git cherry-pick --abort
That rolls back to the state before the cherry-pick started.
Finally, don’t forget to remove the remote if you no longer need it.
git remote remove repoB
In fact, it is a different way from cherry-picking, but it achieves our last goal.
If you don’t want to add a remote, you can generate a patch file from the other repo and apply it.
- Generate the patch file:
In RepoB, identify the commit hash you want to pick (as we did before), so export it into a patch file:
So, in RepoB, run:
git format-patch -1 abc1234 --stdout > mypatch.patch
-1 abc1234
means “one commit” (the one we want).--stdout
emits the patch to stdout (redirected to file).
Now you have a physical file (mypatch.patch
) in the RepoB
that represents the commit.
- Move this patch file to RepoA
Copy mypatch.patch
into RepoA (can be via cp
, USB, email, etc.):
cp /path/to/RepoB/mypatch.patch /path/to/RepoA/
- Apply the patch in RepoA
Then in RepoA:
git apply mypatch.patch
This method is useful when you can’t or don’t want to maintain a remote link.
- Avoid cherry-picking too many commits across repos; it may cause duplicated code or merge confusion.
- Always double-check commit context (dependencies); sometimes a commit depends on earlier ones you didn’t cherry-pick.
- After you fetch from an external remote, avoid accidentally merging the remote branch unless you intend to.
In summary, to git cherry-pick from an external repo:
- Add the external repo as a remote
- Fetch it
- Identify commit(s)
- Run
git cherry-pick
(or patch method) - Resolve conflicts
- Push your changes
Finally, if you found this article helpful, you can check more here:
- SQL COALESCE in Postgres: A Simple Guide
- Facade vs Proxy vs Adapter Design Patterns
- TypeScript Type Vs Interface? The Answer Is Type!
Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.