Prachi Sharma
Prachi Sharma

Reputation: 341

Cherry-pick branches from one Repo to another

I have two repositories Repo1 and Repo2. I have few new branches in Repo1 that should be moved to Repo2. How can I cherry-pick those branches(along with all the commits in it) and move it to second repo?

So far I have done:

git remote add repo2 https://...repo1.git  
git fetch other

It listed out all the branches created in Repo1. But I'm not sure how can I cherry-pick those branches and push in Repo2.

Ps. I cannot do git remote since it will cause alot of conflicts as there's some difference in data in both the repositories.

Upvotes: 1

Views: 1692

Answers (1)

VonC
VonC

Reputation: 1325137

First, you fetch not be should be git fetch other but.

cd /path/to/repo2
git remote add repo1 https://...repo1.git  
git fetch repo1

In other words, you would work in a local clone of the target repo (repo2), not in repo1.

Second, it should list all branches, with git branch -avv.
However, since repo1 and 2 have no common history, it would be up to you to determine the start of a repo1 branch (or it would consider the branch commits all the way back to the first repo1 commit: there is no common branch from which said branch has started in repo1)

If you think a repo1 branch can be applied to an existing repo2 history, you can:

  • create a new branch branch from repo1/aBranchToMove
  • rebase --onto the repo1 branch (on)to an existing repo2 branch

That is:

cd /path/to/repo2
git switch -c newBranch repo1/branch
git rebase --onto main <first-Commit>~ newBranch

That will relocate commits, from <first-Commit> (that you need to lookup in repo1) up to newBranch (which references the repo1 branch HEAD, that you want to cherry-pick) onto an existing repo2 branch (here 'main' for instance)

Upvotes: 1

Related Questions