3

I do have a Repo1 and a Repo2 containing Project1 and Project2 respectively. Each repo with its own history:

Repo1 / Project1
   history: A -> C -> G -> L

Repo2 / Project2
   history: B -> F -> R

I would like to create a NewRepo which hosts NewProject. NewProject includes Project1 and Project2 as independent subdirectories. The history of both repos should be merged into a single linear history.

NewRepo / NewProject
            |_ Project1
            |_ Project2
   history: A -> B -> C -> F -> G -> L -> R

Q Git HowTo needed.


I'm aware of the answers below but they don't solve my problem.

Community
  • 1
  • 1
participant
  • 2,923
  • 2
  • 23
  • 40
  • Start by creating a third repo, and push both branches to it (under different names). Then ask yourself: Is there a pattern according to which commits from those two branches should be combined? For instance, you may want to arrange commits in chronological order. In that case, you could follow the procedure outlined at http://stackoverflow.com/questions/25928921/how-can-i-splice-two-or-more-completely-unrelated-linear-branch-ancestries-i/25930104#25930104. – jub0bs Oct 29 '15 at 13:46
  • @Jubobs Both repos are created via `git tfs clone ..` not including branches but still containing an awful lot of merge-commits and so I got stuck with cherry-picking. Unfortunately, I found no way to iron out the merge commits into a single linear history. – participant Oct 29 '15 at 15:56
  • Does this answer your question? [How do you merge two Git repositories?](https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories) – Inigo Jan 29 '23 at 02:30

3 Answers3

0

This is a trivial description that might miss important parts, but maybe it will lead you in the correct direction.

Create the new repo first (assuming the repositories are at the same place in the directory structure)

mkdir -P NewRepo/NewProject
cd NewRepo
git init

Then add your old repositories one at a time

git remote add repo1 ../Repo1
git fetch repo1  # This will add Repo1 including the git history to NewRepo

Then move your branch to the correct place in Repo1. Assuming you have a master branch on top in Repo1 you can

git reset --hard repo1/master

or

git checkout repo1/master

Now you can move your files to the correct directory. Add and commit.

mv Project1 NewProject
git add NewProject
git commit -m 'Moved Project1 to NewProject'

Then it is the same steps for Repo2.

Here you might want to remove the remote repositories.

git remote remove repo1
git remote remove repo2

The history should not be a big issue. gitk --all will show you the history in a correct way. If you want to change the order you can always use git rebase -i.

git rebase -i <oldest commit to handle>

This will start your favorite editor with all commits listed. Here you can rearrange the order of them. When you save and quit the editor git will apply your changes from where you are. That is; the old history will remain.

If you really, really want to change history you also have the command git filter-branch, but that tool is outside of my comfort zone.

Robert Locke
  • 455
  • 4
  • 7
0

It's not exactly what I asked for but it solved my problem satisfactorily.

As a starting point I used this answer.

  1. create NewRepo

    cd NewProject
    git init
    
  2. add Repo1 and Repo2 as submodules like

    git submodule add https://.../Repo1 Project1
    git submodule add https://.../Repo2 Project2
    
  3. reset the sub-modules to a specific snapshot

    cd Project1
    git reset [--hard] C
    cd ../Project2
    git reset [--hard] F
    
  4. commit a snapshot C-F for convenient restore in future

    commit -m 'release x.xx'
    
  5. repeat the previous steps 3,4 as often as you need a synchronization point in time (A-C, G-F, G-R)

  6. restore any synchronization point like

    git reset [--hard] 'release x.xx'
    git submodule update
    
Community
  • 1
  • 1
participant
  • 2,923
  • 2
  • 23
  • 40
0

With git subtree it is as simple as that

cd NewRepo
git init
# commit something pointless to have one commit in NewRepo
git subtree add --prefix=Project1 path/to/Repo1 <branch>
git subtree add --prefix=Project2 path/to/Repo2 <branch>
participant
  • 2,923
  • 2
  • 23
  • 40