2

I need to sync between two different git repos, but both the repos have same project code/files and different package structure.

The problem currently facing is I need to clone and push the changes 2 times, is it possible to do a push to both the repos from a single push

Example

Repo1: com/example1/demo/DemoService.java

Repo2: com/emample2/demo/DemoService.java

(Note: The files will be the same in both the repos, the only change will be the package structure)

Now when I make changes in com/example1/demo/DemoService.java and push from local it should be pushed to both example1 and exmaple2, is this possible? If yes please let me know what are the necessary steps to be done.

Shashi Kiran
  • 362
  • 2
  • 9
  • 27

2 Answers2

0

This is not directly possible, since the change of structure implies a change of history (the tree SHA1 would not be the same)

What you might consider is pushing to an intermediate local Repo1 clone, which would have a local post-receive hook.

That hook would:

  • push to the remote Repo1
  • go to a local cloned Repo2
  • copy the file that was just modified, and make a local commit
  • push to the remote Repo2

That way: one push and the duplicate file is automatically updated in both remote repositories.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That is right way but the hook created will be only in my local right, how do I make it common across the other developers who clone this repo and push the changes – Shashi Kiran Nov 04 '19 at 06:47
  • @ShashiKiran A hook is always local to its repository, for security reason. But if you want to share that process (hook doing the actual push and updating repo2), then you need to setup an on-premise Repo1, accessible by all developers, : everybody would push to that local server (through SSH for instance), and the Repo1 on that local shared server can then update a local clone of Repo2 and push to both remote repositories. – VonC Nov 04 '19 at 07:02
  • is it possible to change the package structure using hooks? like replace example1 with example2 – Shashi Kiran Nov 05 '19 at 04:02
  • @ShashiKiran: sure: the hook script can copy the file anywhere you need in Repo2. Then add, commit and push. – VonC Nov 05 '19 at 05:47
0

An other solution to your problem could be using git submodules (https://git-scm.com/book/en/v2/Git-Tools-Submodules).

You would have one git repo holding your java Code, from the /demo path in you example, and two other repositories, one for each package name.

Could you also describe why you need to use the same code in two different repos with different packages? Maybe there could be an even better way of solving the whole thing.

Romain Prévost
  • 513
  • 2
  • 12
  • this is required for a project. let me try to put in a simpler way - I have demo project working for example company. This example company is being splint into 2 example1 and example2 (hence the need for different package structure with same code is required), for time being the functionalities will remain same in both, over the time the development may kick in separately. – Shashi Kiran Nov 05 '19 at 05:16