Steve
Steve

Reputation: 4097

Merging Related Git Repositories

I have two related repositories and I would like to merge changes from one to the other. My repository was created from an export of an old legacy repository and some changes were made but the structure largely kept so my repo has new files and a few new names but 90% is the same. Now, some fixes had to be made to legacy software and I want to bring the latest changes to the legacy repo in. I have both repositories checked out on my computer, but it is unclear what the best option is.

enter image description here

Some ideas:

  1. I could try and set the legacy repo as a branch and merge it into the main.
  2. I could try and rebase all commits since the archive onto my repo.
  3. I could cherry pick all commits from legacy and then apply them to my repo

What is the most direct way?

Upvotes: 0

Views: 104

Answers (2)

Simon
Simon

Reputation: 379

Rebase is a cleaner solution in the end. All the changes you made in your repo is gonna be placed on top of the latest version of the legacy repo. This way the legacy repo is completely intact in the commit history of your repo.

Upvotes: 0

Schwern
Schwern

Reputation: 164639

First, make the legacy repo a remote of your repo. It can be referenced by a URL or by a file path.

git remote add legacy <path or URL to legacy>

Fetch it.

git fetch legacy

A [legacy/master]
|
B        F [master]
|        |
C        G
|        |
D        H
|
E

At this point we could smack them together with git merge --allow-unrelated-histories but that increases the chance of a bad merge. Instead, first stitch the histories together by rewriting the commits in your new repo onto the commit where they diverged. Let's say that was commit D.

git rebase D

[legacy/master] A
                |  F1 [master]
                B  |
                |  G1
                C /
                |/
                D
                |
                E

Note that the first commit in the new repo should have the same content as D and will probably disappear. If it remains, it should only contain the differences introduced by the copy. These may be significant.

Now merge normally.

git merge legacy/master

                   M [master]
                  /|
                 / |
[legacy/master] A  |
                |  F1
                B  |
                |  G1
                C /
                |/
                D
                |
                E

And make this the central repo going forward.

Upvotes: 3

Related Questions