Skip to content

Instantly share code, notes, and snippets.

@nemotoo
Last active December 6, 2024 16:26
Show Gist options
  • Save nemotoo/b8a1c3a0f1225bb9231979f389fd4f3f to your computer and use it in GitHub Desktop.
Save nemotoo/b8a1c3a0f1225bb9231979f389fd4f3f to your computer and use it in GitHub Desktop.
.gitattributes for Unity3D with git-lfs
## Unity ##
*.cs diff=csharp text
*.cginc text
*.shader text
*.mat merge=unityyamlmerge eol=lf
*.anim merge=unityyamlmerge eol=lf
*.unity merge=unityyamlmerge eol=lf
*.prefab merge=unityyamlmerge eol=lf
*.physicsMaterial2D merge=unityyamlmerge eol=lf
*.physicMaterial merge=unityyamlmerge eol=lf
*.asset merge=unityyamlmerge eol=lf
*.meta merge=unityyamlmerge eol=lf
*.controller merge=unityyamlmerge eol=lf
## git-lfs ##
#Image
*.jpg filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.gif filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.ai filter=lfs diff=lfs merge=lfs -text
#Audio
*.mp3 filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text
#Video
*.mp4 filter=lfs diff=lfs merge=lfs -text
*.mov filter=lfs diff=lfs merge=lfs -text
#3D Object
*.FBX filter=lfs diff=lfs merge=lfs -text
*.fbx filter=lfs diff=lfs merge=lfs -text
*.blend filter=lfs diff=lfs merge=lfs -text
*.obj filter=lfs diff=lfs merge=lfs -text
#ETC
*.a filter=lfs diff=lfs merge=lfs -text
*.exr filter=lfs diff=lfs merge=lfs -text
*.tga filter=lfs diff=lfs merge=lfs -text
*.pdf filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.dll filter=lfs diff=lfs merge=lfs -text
*.unitypackage filter=lfs diff=lfs merge=lfs -text
*.aif filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
*.rns filter=lfs diff=lfs merge=lfs -text
*.reason filter=lfs diff=lfs merge=lfs -text
*.lxo filter=lfs diff=lfs merge=lfs -text
@zloop1982
Copy link

@nemotoo I see you use merge=unityyamlmerge to let git use unityyamlmerge for one type of files, How to let git know the unityyamlmerge tool?
how to config unityyamlmerge in .gitconfig file?

I like your way to set unityyamlmerge for specified files.
but this setting in .gitconfig file:
[merge]
tool = unityyamlmerge
I'm afraid that may let git always use unityyamlmerge for everything.

@Chefty
Copy link

Chefty commented Jan 20, 2021

I agree with @evitolins what about tracking .unity and .asset with LFS?
From what I read here it will only be handled by unity yaml "smart merge"?

I have very large scenes over 100mb and Github is complaining about it. Without talking about lighting datas...
I added these two lines at the end of my .gitattributes:

# Scenes
*.unity lfs
[Aa]ssets/[Ss]cenes/*.assets lfs

But now will the yaml "smart merge" still work?

@upscalebaby
Copy link

I like this one, I'd add HDR image support as well:

*.hdr filter=lfs diff=lfs merge=lfs -text

@benkoadam
Copy link

Since you are using yamalMerge for *.unity, lets say that you have just created a scene with more than 200 MB in size. How will you push it ? Did you increase your git push limit too ?

@Hoodad
Copy link

Hoodad commented Jan 27, 2022

To prevent LightingData.asset to become corrupted I highly recommend to treat it like a binary file. When working we have had multiple occureneces where the LightingData.asset ended up corrupt when pushed through LFS due to the unityyamlmerge

LightingData.asset filter=lfs diff=lfs merge=lfs -text

@fkkcloud
Copy link

fkkcloud commented Apr 14, 2022

with #*.wav filter=lfs diff=lfs merge=lfs -text, when I merge other branches that has changed .wav files, I am getting .wav files "dirty" and listed in the changelist (it is not the conflict btw) every time. if I remove "filter=lfs", it works fine. any ideas?

@omundy
Copy link

omundy commented Apr 21, 2022

Thanks for this. Consider adding .psb files (along with PSDs)

@SuleimanAbdullah
Copy link

Thanks, @Hoodad I have spent weeks to solve this but light data make push large file failed I follow your recommendation and the push worked thanks again you save my day

@Breanzy
Copy link

Breanzy commented Jun 23, 2022

Thanks for this ^^

@Begounet
Copy link

Begounet commented Nov 3, 2022

Months later, got the same issue, because based my .gitattributes on this. Thank you @Hoodad! Should really consider adding this to the file.

@jwvanderbeck
Copy link

As I had pointed out way above, the yaml merge tool is NOT supposed to be used on .asset files. Only .unity and .prefab

@SuleimanAbdullah
Copy link

SuleimanAbdullah commented Nov 4, 2022 via email

@kenorb
Copy link

kenorb commented Mar 9, 2023

For .gitignore, look for GitHub's official version at https://github.com/github/gitignore/blob/main/Unity.gitignore

@NPatch
Copy link

NPatch commented Sep 29, 2023

Seems to me like the right way is:
*.asset ......
LightingData.asset .... -text
*TerrainData.asset .... -text

As @40detectives implied before but yours truly never noticed, since gitattributes uses the latter rule when multiple patterns match a file, it should treat everything as text, unless its name is LightingData.asset or TerrainData.asset etc. I don't define folders because each type of binary .asset has different implications.

TerrainData.asset, unless I'm mistaken, can be renamed safely from within the editor, without corrupting its data so you might want to use a naming convention(hence the * if you do sth like SceneName_TerrainData.asset).

LightingData.asset though, while it can be renamed from within the editor, at least for me, it doesn't work afterwards. Lighting is wrong, kinda like overexposed. Also if you rebake lighting, the result is again stored in LightingData.asset. So regardless of what you do, it will always be like SceneName/LightingData.asset.

There's a third type of binary .asset which is NavMesh. As far as I'm aware these are the 3 types. But I've never used it, so not sure of its quirks. It most likely has some.

So LightingData is always under a folder with the same scene name as the scene it's baked for. TerrainData is likely a free agent, you can put them wherever. NavMesh I assume is also a free agent.

Update:
TerrainData is the name of the type, but the default naming is New Terrain.asset. Still it's renamable so:
*-[Tt]errain.asset or *-[Tt]errainData.asset should be fine.
For LightingData though it has to remain as is and let the folder hierarchy differentiate them.

Update2: I found out about a nifty git command that can help you figure some things out if you're in a weird situation with attributes:
git check-attr

It tells you if a pattern of specific filepath has attributes and which they are. For example, using the above recommended rules:
git check-attr -a .asset
returns:

.asset: merge: unityyamlmerge
.asset: eol: lf

while based on the latter rule I set before:
git check-attr -a LightingData.asset
returns:

LightingData.asset: diff: lfs
LightingData.asset: merge: lfs
LightingData.asset: text: unset
LightingData.asset: eol: lf
LightingData.asset: filter: lfs

The LightingData clearly speficies that "text" is unset, so it's not text type asset, but binary.

The .asset though doesn't specify "text". To further verify you can do:
git check-attr text .asset
and you'll get:

.asset: text: unspecified

Documentation for gitattributes' text states that in the absence of text, it defaults to what core.autocrlf defines, which by default is true, so it treats it as text.

There is a case to be had for "text=auto" in letting git decide if the asset is text or binary, but I can't be sure about it, not having used it.

@FrankNine
Copy link

I wrote a post about that .gitattributes and other Unity with git stuff here:
https://franknine.github.io/posts/unity-git-en/#gitattributes

@NPatch
Copy link

NPatch commented Oct 14, 2024

@FrankNine Add in the NavMeshData to your list of binary .asset. Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment