Home > OS >  How do I sync two repos where the remote is gone
How do I sync two repos where the remote is gone

Time:09-23

I have two new servers where my code is checked out from an old server that no longer exists. So I have two repo's both referring to a nonexisting repo. After that there has been development on both servers. Something like this:

  r1 - was built existed on an old server
       /                      \
r1 - cloned to new1      r1 - cloned to new2
      |                        |
     r2                       r4
      |                        |
     r3                       r5

How do I fix this so that I can push and pull on both servers and the result will automatically end up on the other server.

I want the revisions to be lined up so that its basically the same in both servers:

r1 - r2 - r3 - r4 -r5

I have used the following script to create a copy of my setup:

#!/bin/bash

rm -fr main_test_repo tmpA tmpB

# Create a repo with one file
mkdir main_test_repo
git init main_test_repo
echo "First file" > main_test_repo/file1
git -C main_test_repo add file1
git -C main_test_repo commit -m 'First file added'

# Clone and make some changes
for x in tmpA tmpB; do
    git clone main_test_repo $x
    echo "$x file content" > $x/${x}_file
    git -C $x add ${x}_file
    git -C $x commit -m "$x added"
done

# Make the origin go away
rm -fr main_test_repo

# Add remote that points to the "other" server
git -C tmpA remote add -f tmp_b $(pwd)/tmpB
git -C tmpB remote add -f tmp_a $(pwd)/tmpA

Q: So what git magic is needed here to get those two repos play well with each other?

CodePudding user response:

You want a bare repo to use as the shared remote.

You could just make each existing repo the other's remote, but it will get messy. So, if you don't want to use github or any other externally- or centrally-hosted repo, just create a bare clone of one:

git clone --bare /my/repo/new1/.git /my/shared-remote.git

now add that as a remote for new1:

cd /my/repo/new1
git remote add shared /my/shared-remote.git

(and set shared as the upstream for whatever branches you have, with git remote set-branches shared <branchname> or git branch --set-upstream ...)

finally add it as a remote of new2 as well, and decide how to merge their changes.

I'd probably rename the duplicated branch, push it to shared and then merge them together, but rebasing is fine if you don't mind rewriting history.

Note that where I've written a local path, it can just as easily be a URI so long as you have SSH, or a git server, or some other way of accessing the remote.

  •  Tags:  
  • git
  • Related