'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.



Solution 1:[1]

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.

Solution 2:[2]

simple way of mirroring a directory of git repository into another git repository

git clone --mirror [email protected]/mirror-repository.git

cd mirror-repository.git

push changes to new repository using below command

git push --mirror [email protected]/new-mirror.git

This will get all the branches and tags that are available in the mirror repository and will replicate those into the new location.

Don’t use git push --mirror in repositories that weren’t cloned by --mirror as well. It’ll overwrite the remote repository with your local references (and your local branches).git clone --mirror is prefered over git clone --bare because the former also clones git notes and some other attributes.

Solution 3:[3]

If RepoB is going to have just /amp content than it is more correct to make a library from it. Put this library as dependency to your project in RepoA. How to do it depends on language (package manager) that you use.

For dev environment you can create symlink in RepoA that will point to RepoB.

Solution 4:[4]

If submodule are not to your liking, an alternative approach would be a content filter driver in a .gitattributes of repoA, for all files in /amp/.

See Pro Book:

https://git-scm.com/book/en/v2/images/clean.png

More specifically, a "clean" filter would be called automatically on git commit (only for /amp/** files), and could then perform the necessary command in order to replicate the changes in repoB.

That clean merge driver script would be:

#!/bin/sh
git --work-tree=/path/to/repoA/amp --git-dir=/path/to/repoB/.git add .
git --git-dir=/path/to/repoB/.git commit -m "import from repoA/amp"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthieu Moy
Solution 2 Shah E Rome Wali
Solution 3 Raz
Solution 4 VonC