Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mirror a directory in a git repo into another git repo?

I have a directory /amp in RepoA and a RepoB (populated with the contents of /amp initially). I want to mirror any changes to the /amp in RepoA into RepoB.

Is it possible ?

P.S: RepoA contains many other directories. I only want to mirror the /amp dir. Whereas RepoB is only going to have the /amp contents not any other.

like image 671
mukilane Avatar asked Jan 19 '17 07:01

mukilane


2 Answers

nbari's answer is correct that submodules are designed to solve this problem.

One potential drawback of submodules is that they are not fully transparent to the user. If you want some people to be able to use RepoA and RepoB without having to know anything about submodules, an alternative is git subtree. The main commands you would have to use are:

  • git subtree split, that can extract subdirectory amp from RepoA to a repository of its own, say RepoAMP (you may also use git subtree push to push the result).

  • git subtree merge that can merge RepoAMP to directory /amp in RepoB (you may also use git subtree pull to download and merge in one command).

In the end, RepoA and RepoB are both "normal" repositories. The mirroring is not done automatically but git subtree helps you doing it properly.

like image 134
Matthieu Moy Avatar answered Oct 30 '22 03:10

Matthieu Moy


You could use git submodules for this, the first step would be to create a repository from your dir /amp and later use it as a submodule in your other repositories.

To make /amp a repository you could do:

$ cd /path/to/amp
$ git init
$ git add .
$ git commit -m "First commit"

Now let's assume you will be using github, you could publish your repo by doing:

$ git remote add origin [email protected]:<your-user>/amp.git
$ git push -u origin master

Now on repoA and repoB you will need to add the new amp repo as a submodule, this can be done by doing something like this:

$ cd /path/to/repoA
$ git submodule add -b master [email protected]:<you-user>/amp ampMirror

Same for repo repoB

$ cd /path/to/repoB
$ git submodule add -b master [email protected]:<you-user>/amp ampMirror
$ git commit -m "added appMirror submodule"

Notice that the name I am giving to the directory within repoA and repoB is ampMirror but it can be anything you want, indeed you can use the same name by just using:

git submodule add -b master [email protected]:<you-user>/amp 

The directory structure would look something like:

repo(A/B)
├── .git/
├── .gitmodules   <-- created after adding the submodule
├── foo
└── ampMirror
    ├── .git/     <-- repo to keep/track changes for app (need to git pull)
    └── foo

Now every time a change is made into your amp repo you will need to update "pull" the changes in your repositories for example:

$ cd /path/to/repoB
$ cd appMirror
$ git pull
$ cd ..    <--- get back to your project root
$ git commit -am "update submodule" 

Another way of doing this and become useful if have more than one submodule is:

$ cd /path/to/repo(A/B)
$ git submodule foreach git pull origin master
$ git commit -am "updated submodules"

I hope this can help to give you a better idea.

like image 33
nbari Avatar answered Oct 30 '22 03:10

nbari