Question

How can I copy a chain of commits from one git (or svn) repository to another?

If I would have only one commit I would do:

 cd Git1
 git diff commitHash^ commitHash > patch.txt
 cd Git2
 git branch CommitsFromGit1
 git checkout CommitsFromGit1
 patch -i ....Git1/patch.txt
 git ci -am "CommitFromGit1Name"

But I have too many commits to proceed manually one by one. Is there a way to create somekind of multi-patch, with a set of commits-changes?

Was it helpful?

Solution

Let's call your gits A and B and say you're working on the master branch. While in git A, add B as a remote and fetch its contents into A:

git remote add B /path/to/B
git fetch B

(You don't have to add it as a remote; you can just do git fetch /path/to/B master:B/master if you like. This copies all branches in B which might be unnecessary in your case. Feel free to specify an explicit refspec to limit the fetch.)

Now you can use git-cherrypick to copy individual commits from B to A, or use git rebase --onto to copy multiple commits. The latter command has this general form:

git rebase --onto destination boundary starting_point

Git walks backwards from starting_point until it reaches boundary and copies the resulting commits (excluding boundary) onto the tip of destination. Thus, this copies the three most recent commits from B's master branch to A's master branch:

git rebase --onto master B/master~3 B/master
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top