git 忽略版本控制上的文件夹元文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19151247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:05:20  来源:igfitidea点击:

Ignoring folder meta files on version control

gitversion-controlmercurialunity3d

提问by Roberto

Unity creates and deletes meta files for foldersinside the Asset folder.

Unity 为Asset 文件内的文件创建和删除元文件。

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, theirUnity 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.

这会在使用版本控制时产生一个恼人的情况(您可以跳过并转到问题):有人创建了一个文件文件夹,该文件夹将被忽略但忘记忽略该文件夹的元文件。Unity 创建元文件,此人将元添加到版本控制。另一个人获得了变更集,由于他们没有文件夹,他们的Unity 删除了元文件,并从版本控制中删除了元文件。并非团队中的每个人都理解这一点,因此该过程在地狱般的循环中永存。

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?
  1. 版本文件夹元文件很重要吗?
  2. 有没有办法自动忽略文件夹元文件 - 特别是在 git 或 mercurial 上?

采纳答案by Kay

The Unity docssay:

统一文档说:

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

创建新资产时,请确保资产本身和关联的 .meta 文件都添加到版本控制中。

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 hookon 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 命令,以下可能有用: git hook 脚本应该首先检查 .gitignore 是否已通过以下方式更改:

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:

如果 .gitignore 中所有新添加的行位于 Assets 文件夹下,则打印它们的目录名称:

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-assetsfor code and my blog postingabout it for more details.

我刚刚写了这样一个预提交钩子 :-) 请参阅 GitHub 存储库git-pre-commit-hook-unity-assets代码和我的博客文章了解更多详细信息。

回答by Guo Lieene

Add this to .gitignore

将此添加到 .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.

这将忽略没有后缀的文件。但这不应该受到伤害。

回答by razblack

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

要包含元文件和资产,只需在排除项后添加以下内容:

!*.*.meta

Here is a sample of my .gitignore:

这是我的 .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:

我将它放在具有 git 存储库结构的文件夹中,因此我的项目结构将类似于:

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

hope that helps.

希望有帮助。