Ignoring folder meta files on version control

GitVersion ControlMercurialUnity3d

Git Problem Overview


Unity creates and deletes meta files for folders inside the Asset folder.

That can create an annoying situation when using version control (that you can skip and go to the questions): someone creates a folder of files that will be ignored but forget to ignore the folder's meta file. Unity creates the meta file and this person adds the meta to version control. Another person gets the changesets and, since they don't have the folder, their Unity deletes the meta file and they remove the meta file from version control. Not everyone in the team understand this, so the process is perpetuated in a loop from hell.

Surprisingly this happens all the time. So, two questions:

  1. Is it important to version folder meta files?
  2. Is there a way to automatically ignore folder meta files - particularly on git or mercurial?

Git Solutions


Solution 1 - Git

The Unity docs say:

> When creating new assets, make sure both the asset itself and the associated .meta file is added to version control.

For me this is reason enough to put them under version control. I see two approaches to solve the problem:

  • Organisational: Set up a naming convention for local folders like starting with "_". But we all know that this won't work ;-)
  • Install a client side pre-commit hook on all machines. I haven't done this yet but it seems promising.

I just played around with the different git commands, the following could be useful: The git hook script shoud first check if .gitignore has changed by:

git diff-index --cached --name-only HEAD | grep ".gitignore"

Print out directory names of all newly added lines in .gitignore if they are located under the Assets folder:

git diff --cached --word-diff=plain .gitignore | grep -o -e "{+\/.*\/Assets\/.*+}" | cut -d + -f 2

Update

I just wrote such a pre-commit hook :-) See the GitHub repository git-pre-commit-hook-unity-assets for code and my blog posting about it for more details.

Solution 2 - Git

Add this to .gitignore

#Ignore all .meta file
*.meta
#But not source file with postfix. which is everything but a folder
!*.*.meta

This will ignore file without postfix. But that shouldn't hurt.

Solution 3 - Git

Yes, .meta files are important. They so some useful tasks for you like keeping GUID of your files and folders so Unity can keep references even when a file/folder is renamed or relocated. See full discussion

You need to add

# Meta Files built by Visual Studio
*.meta

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Autogenerated files
InitTestScene*.unity.meta
InitTestScene*.unity

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

into your .gitignore. Then as usual, refresh your gitignore .

Since last line keeps (un-ignores) meta files that is under Assets folder, meta files of the folders inside Assets folder are not ignored.

Solution 4 - Git

to include meta files along with assets, simply add the following after your exclusions:

!*.*.meta

Here is a sample of my .gitignore:

# Ignore the following files
# --------------------------
# Unity3D specific
#
**/Library/*
**/Temp/*
**/*.csproj
**/*.unityproj
**/*.sln
**/*.userprefs
**/*.pidb

# include meta files
!*.*.meta

I place this at the folder with the git repository structure, so my project structure would look similar to:

root folder /
   - Unity Project/
        - .gitignore
        - .git/
        - ProjectFolder/
              - {all project related data}

hope that helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertoView Question on Stackoverflow
Solution 1 - GitKayView Answer on Stackoverflow
Solution 2 - GitGuo LieeneView Answer on Stackoverflow
Solution 3 - GitOnat KorucuView Answer on Stackoverflow
Solution 4 - GitrazblackView Answer on Stackoverflow