Yes and no.
Yes, you can share subdirectories between branches - git does this all the time.
No, you cannot sync them automatically using regular git commands.
Unfortunately, I'm not close to the shell right now, so I cannot provide commands, but I will show you the way.
Git has three main data structures:
- blobs are objects that contain the actual raw byte streams of your files or other things.
- trees are objects that describe a directory, as in a regular file system. They contain drop hashes along with their name, mode, etc.
- commits are objects containing a hash of a tree object and sorted information, such as author, timestamp, parent commit, etc.
These three things literally represent a file in .git/objects
with a file name equal to their hash content.
For your problem, the key is that these objects build a read-oriented acyclic graph, like you, using commits.
You are interested in trees. After scrolling with git cat-file
you can take a look at some tree objects. You will immediately notice how this will solve your problem if you were able to crack the _include
tree _include
that it contains pointers to trees from this other branch. Note that at this deep level, it's natural to share trees between commits and branches, git does this all the time. Among other reasons why repositories take up relatively small space, and also why git is sometimes called a distributed file system.
Now you have two adventures left:
- Learn how to fake a tree similar to what you need. As I said, I donβt have a shell now, but you should understand this with examples in https://git-scm.com/book/en/v2/Git-Internals-Git-Objects , which show how to read and write individual objects.
- Unfortunately, due to the nature of these read-only data structures, you need to
_include
tree every time one of the included trees changes, as this will also give them new hashes. You probably guessed it - nothing can really change any git object. Changes always arise by creating new objects. This should not be a big problem, but you can create an appropriate hook for your script, which automatically creates a new tree for you.
Frankly, as exciting as all of this, you can just forget everything I just said and just recreate _include
from scratch with git china commands in the hook every time a commit affects the source directories by checking them from source branch, then copy (using cp
, not some special git command) and send them to the destination branch. git will actually automatically detect that these subdirectories match what was already in their respective objects, so the end result will be virtually the same as if you yourself created the tree object!
source share