Prefabs in git

Hi, guys.

My team experienced really unpleasant problem: we are storing some ui prefabs in our project, but every time someone saves scene he gets a bunch of prefabs, marked as changed in git. The problem is that no one is changing prefabs.

Has any one experienced this and found a solution?

Thanks in advance

thanks for your reply :) I understood how unity saves our prefabs. but even when I do "Save Project" git marks prefabs as changed. another strange thing that diff shows that prefab is unmodified. that is : git sees prefab as modified, but diff says that it's not. and we're already using text-based serialization

–

There must be some difference or git would not claim to see one. Is it a CRLF issue perhaps?

–

agreed. source tree shows that nothing is changed "unmodified file", but git says that this file is modified. take a look [21039-untitled.png|21039]

–

I don't know what frontend you're using there, but it looks like there's an "ignore whitespace" option at the top, did you try setting it to see if it's just a whitespace change? Does git itself think the file is modified? You might be able to get more information out of it directly. Does "git status" show the file, and does "git diff" explain the differences?

–

this is source tree. yeah, git status marks this file as modified. git diff doesn't launch external merge tool, however launches it for all really changed files. that looks like some kind of magic.

–

1 Answer

1

This problem can be caused by a script modifying data in OnValidate(). If the data is modified unconditionally, it seems like the prefab will be marked as dirty in memory despite no changes actually being made. It will then be written to disk when the scene is next saved, triggering your source control plugin to mark the file for edit.

I’ve reproduced this effect using a prefab with a script containing this code:

#if UNITY_EDITOR
void OnValidate()
{
	transform.rotation = Quaternion.identity;
}
#endif

The problem can be fixed by changing the code to this:

#if UNITY_EDITOR
void OnValidate()
{
	if (transform.rotation != Quaternion.identity)
		transform.rotation = Quaternion.identity;
}
#endif

I was also initially confused by the whitespace differences I saw in source control. I’m using Perforce on Windows with the settings set to sync from the server with native line endings (CRLF), but Unity always seems to always write text-based assets with Unix-style line endings (LF). That means that files last touched by source control may not have the same whitespace as files last touched by Unity.