Page 1 of 1

Debugging Merge Conflicts: .gitignore and folder structur

PostPosted: Mon Apr 17, 2017 8:55 pm
by BenChang
One thing we've all learned, usually the hard way, is the importance of the .gitignore file when doing projects in Unity, Unreal, or most other game engines. There are just tons of auto-generated files that you do not want in your commits, because they will break things.

With Unity, the Library directory is one example. This entire directory should not be in your repository, because it contains files that are autogenerated on everyone's computer. Overwriting them with Library files from another copy will clobber all kinds of things in your scene; not while not overwriting them will prevent you from committing other changes because Git thinks you have merge conflicts. It's essential that Library/ be untracked, that is, ignored completely by Git.

Symptoms of having your Library accidentally in your repository include:
  • You can't check out or pull any changes, because you get lots of errors like this:
    Code: Select all
    error: Your local changes to the following files would be overwritten by checkout: Library/metadata/1d/1d3b876f5ad72a5499d5bc9a099020a
  • Broken or vanished prefabs
  • Stuff disappears out of your scene
  • "Missing component" shows up in place of scripts on your prefabs, even though they were just there
  • Nothing works :evil: :evil: :evil: :evil: :evil: :evil:
  • If you browse the repo on the bitbucket or github website, you can see the Library folder in there

Why is the Library there?? You surely set up a .gitignore when you created the repository, so what the heck?

Take a look at the .gitignore and verify it really is there.

It commonly looks like this:

Code: Select all
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/Assets/AssetStoreTools*

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


/[LI]brary/ means to ignore Library. The trailing slash means only match directories named Library, not regular files. The square brackets match either capital L or lower-case l, so either Library or library.

The prefix slash is important too, and this is where problems can come from. The leading slash only matches patterns relative to the .gitignore file. So if your .gitignore is in the top level of your Unity project, it will match the Library/ directory, but not, say, Assets/Models/Environments/Town/Library/.

This is fine if your .gitignore is place in the top level of your Unity project.

You may have a directory structure like this, however:

  • OurGame/
    • README
    • .gitignore
    • ArtStuff/
      • Concept Art/
      • MayaFiles/
    • Writing
    • OurGame_UnityProject

      • Assets
      • Library

since .gitignore is above the level where the Unity project is, the Library directory won't be matched. It'd have to be [code]/OurGame_UnityProject/[Ll]ibrary/[code] to match it.

An easy solution is to put the .gitignore into the Unity project directory. You can have it in both places. A .gitignore in any subdirectory will add on to and override anything set in parent directories.

For more about the .gitignore syntax, the shell globbing and regex syntax it uses, etc:

https://git-scm.com/docs/gitignore
http://stackoverflow.com/questions/24139478/when-to-use-leading-slash-in-gitignore
http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm
http://linuxcommand.org/man_pages/grep1.html