Search code examples
gitunity-game-enginegit-subtree

How do I stop ignoring a file pattern in a subtree subdirectory?


I am using Git for a Unity project of my own (I am the only developer, so I can use destructive commands).

As Unity developers may know, Unity creates ".meta" files for everything under the "Assets" folder, which is the main folder of the project. And these files "must" also be tracked along with original files to keep integrity of the project.

Here is the .gitignore file of the project repository:

# Unity Project Folder
/ExampleProjectName/[Ll]ibrary/
/ExampleProjectName/[Tt]emp/
/ExampleProjectName/[Oo]bj/

# Autogenerated VS/MD solution and project files
ExportedObj/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd

# Unity3D Generated File On Crash Reports
sysinfo.txt

# OS Related Stuff
desktop.ini
.DS_Store
.DS_Store?
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

I also have a framework and common code repository. And I ignore the .meta files in this framework repository, because the .meta files for these scripts should be generated by a per-project basis when the framework is imported into project. (And it is annoying to see .meta files everywhere in the framework repo.)

Here is the .gitignore file of the framework repository:

# Exclude ".meta" files created by Unity
*.meta

# Autogenerated VS/MD solution and project files
### The rest is identical to the project .gitignore file. ###

As you may imagine I tried to add the framework as a subtree to project repository. It actually works, but there is a problem: The .gitignore file of the framework repository which ignores .meta files, also applies to project repository and causes it to ignore .meta files under the subtree's directory.

To sum up: I want the .meta files that are in the subtree directory (recursively) to be tracked on the project repository, but to be ignored on the framework repository.

(For those wondering why .meta files must be tracked, the most important reason is .meta files include the ID of respective asset, and Unity uses these IDs in order to referance these assets in project.)


Solution

  • You can add a .gitignore file in your Asset directory that overrides and negates the inherited ignorance with !

    I'm a little confused by your file structure hierarchy, so here's an example to demonstrate.

    .
    ├── .gitignore
    ├── main.meta
    └── unity
        ├── .gitignore
        └── sub.meta
    

    Contents of root .gitignore

    *.meta
    

    Contents of unity/.gitignore

    !*.meta
    

    Now let's see what git wants to track:

    git add . && git status --ignored
    
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
      new file:   .gitignore
      new file:   unity/.gitignore
      new file:   unity/sub.meta
    
    Ignored files:
      (use "git add -f <file>..." to include in what will be committed)
    
      main.meta
    

    You can see that it's tracking the Unity meta files, but ignoring them everywhere else.