78

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?
Roberto
  • 11,557
  • 16
  • 54
  • 68
  • Are the meta files in the same folders as the files? (as described in http://docs.unity3d.com/Documentation/Manual/ExternalVersionControlSystemSupport.html) – VonC Oct 03 '13 at 06:38
  • 3
    I found your question so inspiring that I wrote such a pre-commit hook script. Look at [git-pre-commit-hook-unity-assets](https://github.com/kayy/git-pre-commit-hook-unity-assets/tree/master) for more information – Kay Oct 03 '13 at 12:57
  • 1
    As of 2016 there's a nice plugin that purges empty folders whenever you save a scene https://www.assetstore.unity3d.com/en/#!/content/24284 – pal Oct 04 '16 at 09:39
  • @pal know of any newer alternatives? I get an incompatability warning with Unity 2018 –  Jun 15 '18 at 19:38
  • 1
    Afraid not. In Mercurial I've been ignoring things like `.+/[^\.]+\.meta` since it would only match `.meta` files for things with no `.` in their name. – pal Jun 19 '18 at 19:09

4 Answers4

72

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.

Black
  • 18,150
  • 39
  • 158
  • 271
Kay
  • 12,918
  • 4
  • 55
  • 77
  • The point is, folder itself is not really an asset. And unity should not create meta file of it if possible – Thaina Yu Jun 13 '19 at 03:29
10

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.

Roberto
  • 11,557
  • 16
  • 54
  • 68
Guo Lieene
  • 113
  • 1
  • 7
0

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.

Onat Korucu
  • 992
  • 11
  • 13
-5

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.

razblack
  • 131
  • 1
  • 12
  • since the "!" is used as an exceptional include, removing that would provide the exclusion. so to exclude a folder, it would be similar to : **/*.*.meta/* (to exclude meta folder and subsequent files) or just **/*.*.meta (to exclude all folders with a dot meta path) – razblack Jan 11 '14 at 22:25
  • obviously, but if it needed to you could... not sure why like you said, it would be a disaster in your repo. – razblack Jan 11 '14 at 22:30
  • ah ok, i see what you are actually refering to now after looking at my own stuff, I can see where that would be a problem... I like what Kay created to check the pre-commit. nice work. sorry, i can't vote up yet.. not enough rep :( – razblack Jan 11 '14 at 22:43
  • @Roberto I'm sorry, I'm confused. `.gitignore` is meant for exactly the purpose of not committing those files. This solution is close (`**/*.meta` should do it...right?) and is much better than a precommit hook, or am I missing something? – chacham15 Jul 25 '18 at 20:38
  • @chacham15 You can't ignore all meta files, they are necessary for Unity - except the folder meta files. – Roberto Jul 26 '18 at 00:05