2

I've got several (>10) git repositories that need to have multiple bare repositories on different machines. All copies need to always remain in sync with each other with minimal administrative overhead across all machines that access them.

The current solution I was exploring was to set multiple origins like this:

git remote add origin ssh://host1.domain1.com/home/git/project.git
git remote set-url --add origin ssh://host2.domain2.com/z/project.git

Initially, this works great. Every time changes are pushed (git push -u origin master) all copies of the repo as updated, and everything always remains in sync.

However, when I went to another machine and cloned the repository I was in for a rude shock. When I went to push changes, it only went to the repo I had cloned from. Had I not noticed this and immediately fixed the issue, a serious issue would have developed as pulls from the other repo silently got out of date code. I guess it's understandable that you wouldn't want to hard-code origins in the .git/config file, but it's annoying that there doesn't seem to be any way to do it if you really want to. However, there must be some best practice for accomplish the overall goal, right?

Now I suppose it is possible on each and every single client that ever clones a repo to require that the additional "git remote set-url -add..." command is run. But this assumes a lot. The person performing the pull must know which origins the particular repo they are clone has (they aren't all the same). They must remember to run at least one extra command to add the extra origin. They must formulate the command correctly (e.g. if they pull from host1 they must add host2, but if they pull from host2 they must instead add host1). And they must successfully do all of this for every single clone they do on every single machine they do it on. This gets to be an administrative nightmare.

I have looked for various solutions and some suggest running some kind of script after the clone. This doesn't particularly seem cross platform (now a script must be developed to run on every platform in use) nor fool-proof (how to make sure that it is always run? how to intuit which origin(s) to add?)
Likewise, I would like to avoid needing to run hooks on the server, as no script is going to run cross platform on every server without a lot of development time.

I keep thinking I must be missing some simple way to accomplish this that doesn't require scripts.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Michael
  • 9,060
  • 14
  • 61
  • 123

1 Answers1

1

Yes, a git remote would modify the local config of the repo, which, as its name suggests, is and remains local (for security reason).
That same configuration won't be there on a clone of that repo on another machine.

What would be more interesting is to select one of those remote repos as the gatekeeper one, which everybody clone and push to.
That remote repo can then have a post-receive hook in charge of pushing to the other machines bare repos.

Or: each developer clone from a particular remote bare repo, and each remote bare repo has a post-receive hook pushing to all other bare repos (but the risk of conflicts is higher).

In short: enforcing a policy is best done at the remote side, not locally.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250