2

TL;DR: I want a submodule to ignore some files, but I want those files tracked by the parent project

Details

This is for a Unity project. I want to add a bunch of code and assets I frequently used into a submodule, so I can easily share it between projects.

The problem is that Unity needs to keep track of ".meta" files, that get stored along each asset. And these .meta files are unique for each project, so they can't be shared through the submodule.

So, I added a .gitignore file into the submodule, telling it not to track .meta files

So far things go as planned. I added the submodule into the project, and it correctly ignores the project's meta files. However, although I need to commit those into the main git repository, anything under the submodule is getting ignored, so I can't add the meta files into the main repository.

Example:

Assets
L Game Files
  L some_asset.jpg
  L some_asset.jpg.meta
L Submodule    
  L shared_asset.jpg
  L shared_asset.jpg.meta

In this scenario, I want the main git repository to track:

Assets
L Game Files
  L some_asset.jpg
  L some_asset.jpg.meta
L Submodule    
  L shared_asset.jpg.meta

And the submodule:

shared_asset.jpg

I combed the web, I tried different .gitignore combinations but nothing works. I'm starting to think this is probably impossible, but if anyone has any idea, I would greatly appreciate it!

Thanks in advance!

2 Answers 2

1

Summary: Export Unity packages instead of using git submodules.

The reason they're getting "ignored" in your parent project is not because of your gitignore, but because a submodule is merely a reference to a commit on a repository, not a fork of the repository. In other words, if you want the submodule as a directory in your project, you can't add meta files to it without committing to the submodule repository because the entire directory is in a different repository. You could make a fork of the submodules exclusively for each Unity project, but that will quickly become messy and inconvenient.

Instead, you should be using a custom Unity package. Export your submodules repository as a package in Unity and then open the package when you have a new Unity project you want to use it in. Unity will copy the files into your new project and add the appropriate meta files. You can reimport an updated version of the package in the future if you want to overwrite your new project's assets with the upstream assets. See Exporting Packages in the Unity manual.

2
  • That would be awesome if the submodule / shared assets where already fully mature. However, it gets retouched and updated as I work on the project, so having to update two projects (the game and the shared assets) simultaneously seems inconvenient. Thanks for the advice, though. I might have to end up doing something like that in the end anyways :)
    – Arkaid
    Commented Dec 2, 2017 at 4:19
  • So you need to work on these two projects and their assets simultaneously? Could you finish the assets first before developing the projects or temporarily bring them into a single project with two scenes which you could filter-branch out later? Commented Dec 2, 2017 at 5:37
0

One trick would be to register (in a .gitattributes declaration) in your submodule repo a content filter driver.

smudge (image from "Customizing Git - Git Attributes", from "Pro Git book")

The smudge script, associate to *.jpg, would generate (automatically on git chekcout) the meta file by looking in a parent pre-agreed path (for instance ../meta/myfile.jpg.meta) which is located (and tracked) in the parent repo. The generated actual meta files remain ignored (by the .gitignore).
That means your actual working tree of the submodule does not get "dirty".
And your parent repo does track those meta files.

See a complete example at "git smudge/clean filter between branches".

Not the answer you're looking for? Browse other questions tagged or ask your own question.