Table of Contents Show
If you are working on a large project with multiple branches, you will sooner or later stumble over merge conflicts. Resolving them can be difficult for script files. But for Unity scene files, it might be impossible to resolve the conflicts.
The best strategy is to prevent conflicts in the first place. Here are a few tips to make merging a lot easier.
Use prefabs
Do not place basic objects directly in the scene file. Structure your project into prefabs. These are stored in separate files. When you change something inside a prefab, only this file needs to be merged.
By distributing the content on multiple files, it’s less likely that people will work concurrently on the same file.
Of course, also prefabs can contain merge conflicts. Therefore, divide the prefabs further with nested prefabs. These are prefabs inside of other prefabs.
Use multiple scenes
You can also split your project into multiple scenes. This is especially useful if something is loaded later. The benefit again is, that changing something in one scene does not affect other scenes.
In addition, having multiple scenes enforces a clear separation of different parts of your project.
Consider other branches
Sometimes it still happens that a scene or prefab file is concurrently changed in different branches. Good communication and time management can help here.
It’s good to know what other people are working on. If you know that a colleague is refactoring a prefab, you better wait before doing a bug fix on that same prefab.
Conversely, inform your colleagues if you are heavily changing a part of your project.
Use small and short-lived branches
This is not specific to Unity, but applies to every software project. Don’t work for weeks on a branch. And don’t change a ton of files in one branch.
Try to split a feature into subtasks if it is too big.
Integrate the main branch
Having a short-lived branch is the goal. But sometimes, it just takes a bit longer.
In this case, merge the main branch into your development branch. This way, the branches do not diverge too much. If someone else has changed something in a scene file, you can adopt these changes early.
Do not push inadvertent changes
It happens quite often. A scene or a prefab is changed although you actually didn’t change anything. Sometimes a script changes something. In the UI, layout elements are constantly updated without looking any different. Or you just tested a different value.
Investigate the diff and make sure you really have worked on that file. And you really want to commit the change.
What to do in case of a conflict
It happens. No matter how much you try to avoid it, sometimes there is a merge conflict.
First, take a look at the diff. Sometimes the diff is small and understandable to humans. Then you can try to fix it with a merge tool. Make sure the scene is not broken afterwards and everything works as expected.
Try the SmartMerge tool from Unity. It can be integrated with Git. Again, make sure everything still works after merging.
In some cases, it’s easier to redo your changes. Resolve the conflict by taking the other branch, commit, and then create a separate commit with your changes.