Ignoring folder meta files on version control

52,033

Solution 1

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

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.

Share:
52,033
Roberto
Author by

Roberto

Computer Engineer, MEng.

Updated on July 05, 2022

Comments

  • Roberto
    Roberto almost 2 years

    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
    Roberto over 10 years
    Awesome! Thank you, Kay!
  • razblack
    razblack over 10 years
    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
    razblack over 10 years
    obviously, but if it needed to you could... not sure why like you said, it would be a disaster in your repo.
  • razblack
    razblack over 10 years
    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 :(
  • chacham15
    chacham15 almost 6 years
    @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?
  • Roberto
    Roberto almost 6 years
    @chacham15 You can't ignore all meta files, they are necessary for Unity - except the folder meta files.
  • Thaina Yu
    Thaina Yu almost 5 years
    The point is, folder itself is not really an asset. And unity should not create meta file of it if possible
  • Roberto
    Roberto over 4 years
    this is actually awesome! 🤔
  • PNarimani
    PNarimani almost 4 years
    +1. But while this is good with most of the cases, if your folder includes a dot (.), like in Android plugins, then the folder meta data is going to be included anyway.