DEV Community

Cover image for Two Ways To Git Cherry-Pick From Another Repo
Mohamed Mayallo
Mohamed Mayallo

Posted on • Originally published at mayallo.com

Two Ways To Git Cherry-Pick From Another Repo

Introduction

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.

Why cherry-pick from an external repo?

  • 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 Way: Using Cherry-Pick

First of all, suppose your current repo is RepoA, and you want a commit from RepoB (on GitHub or Locally).

1. 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
Enter fullscreen mode Exit fullscreen mode

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.

2. Fetch from that remote

git fetch repoB
Enter fullscreen mode Exit fullscreen mode

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.

3. 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
Enter fullscreen mode Exit fullscreen mode

Once you see the hash (say abc1234), that’s the commit you want to pick.

4. Cherry-pick the commit

Switch to the branch in RepoA where you want to apply the change:

git checkout my-feature-branch
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Second Way: Using format-patch

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.

1. 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
Enter fullscreen mode Exit fullscreen mode

-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.

2. 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/
Enter fullscreen mode Exit fullscreen mode

3. Apply the patch in RepoA

Then in RepoA:

git apply mypatch.patch
Enter fullscreen mode Exit fullscreen mode

This method is useful when you can’t or don’t want to maintain a remote link.

Keep In Mind These Tips

  • 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.

Conclusion

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:

Top comments (0)