Unity Git 项目解决合并冲突

C#

浏览数:153

2019-8-30

AD:资源代下载服务

Unity项目使用git进行版本控制,最头疼的就是二进制文件。为此,Unity官方开发了YAMLMerge.exe工具,随着Unity一同发布。使用这个工具,需要进行一些设置。

  1. 安装一款合适的 fallback merge tool,作为Unity Automerge 失败后的补救措施。推荐Diff Merge
  2. (如果安装了Diff Merge可跳过这一步)打开Unity安装目录D:\Program Files\Unity\Editor\Data\Tools。找到这个文件mergespecfile.txt.
#
# UnityYAMLMerge fallback file
#

# Modify the next two lines if scene or prefab files should fallback
# on other that the default fallbacks listed below.
#
# %l is replaced with the path of you local version
# %r is replaced with the path of the incoming remote version
# %b is replaced with the common base version
# %d is replaced with a path where the result should be written to
# On Windows %programs% is replaced with "C:\Program Files" and "C:\Program Files (x86)" there by resulting in two entries to try out
# On OSX %programs% is replaced with "/Applications" and "$HOME/Applications" thereby resulting in two entries to try out

# 这两行可替换
unity use "%programs%\YouFallbackMergeToolForScenesHere.exe" "%l" "%r" "%b" "%d"
prefab use "%programs%\YouFallbackMergeToolForPrefabsHere.exe" "%l" "%r" "%b" "%d"

#
# Default fallbacks for unknown files. First tool found is used.
#

# Apple File Merge
* use "/usr/bin/opendiff" %r %l -ancestor %b -merge %d

# Beyond Compare
* use "%programs%\Beyond Compare 4\bcomp.exe" "%r" "%l" "%b" "%d"
* use "%programs%\Beyond Compare 3\bcomp.exe" "%r" "%l" "%b" "%d"
* use "%programs%/Beyond Compare.app/Contents/MacOS/bcomp" "%r" "%l" "%b" "%d"
* use "/usr/bin/bcompare" "%r" "%l" "%b" "%d"

# Araxis Merge
* use "%programs%\Araxis\Araxis Merge\compare.exe" /3 /a2 /wait /title1:"Other" /title2:"Base" /title3:"Local" "%l" "%b" "%r" "%d"
* use "%programs%/Araxis Merge.app/Contents/Utilities/compare" -3 -a2 -wait -title1:"Other" -title2:"Base" -title3:"Local" "%l" "%b" "%r" "%d"

# Perforce merge
* use "%programs%\Perforce\p4merge.exe" "%b" "%r" "%l" "%d"
* use "%programs%/p4merge.app/Contents/Resources/launchp4merge" "%b" "%r" "%l" "%d"

# PlasticSCM merge
* use "%programs%\PlasticSCM5\client\mergetool.exe" -b=%b -s=%l -d=%r -r=%d
* use "%programs%\PlasticSCM4\client\mergetool.exe" -b=%b -s=%l -d=%r -r=%d
* use "%programs%/PlasticSCM/client/mergetool" -b=%b -s=%l -d=%r -r=%d
* use "/opt/plasticscm/client/mergetool" -b=%b -s=%l -d=%r -r=%d
* use "/opt/plasticscm4/client/mergetool" -b=%b -s=%l -d=%r -r=%d

# SourceGear DiffMerge
* use "%programs%\SourceGear\DiffMerge\DiffMerge.exe" --nosplash -m -t1="Incoming Changes"  -t2="Base" -t3="Working Copy" -r="%d" "%l" "%b" "%r"
* use "%programs%\SourceGear\Common\DiffMerge\sgdm.exe" --nosplash -m -t1="Incoming Changes"  -t2="Base" -t3="Working Copy" -r="%d" "%l" "%b" "%r"
* use "%programs%/DiffMerge.app/Contents/MacOS/DiffMerge" --nosplash -m -t1="Incoming Changes"  -t2="Base" -t3="Working Copy" -r="%d" "%l" "%b" "%r"
* use "%programs%/Utilities/DiffMerge.app/Contents/MacOS/DiffMerge" --nosplash -m -t1="Incoming Changes"  -t2="Base" -t3="Working Copy" -r="%d" "%l" "%b" "%r"

这个文件规定了merge失败后用哪个diff merge工具,unity use是解决场景冲突,prefab use是解决prefab冲突。可以根据你使用的Merge工具进行更换。比如,你使用perfoce的话,就把上面那两行换成:

unity use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
prefab use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"

Diff Merge的话就不用管了,它会自动接管merge失败所有后续操作。

  1. Git工程设置
    进入你git工程下的.git文件夹,打开config文件,在文件末尾添加以下内容:
[merge]
 tool = unityyamlmerge
[mergetool "unityyamlmerge"]
 trustExitCode = false
 cmd = 'D:\\Program Files\\Unity\\Editor\\Data\\Tools\\UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"

注意:网上很多人说在工程目录下新建.gitconfig文件,添加以上内容。测试这样不行,而且还有一个问题,这个文件会随着工程一起同步,而大家unity安装目录是不同的,所以基本无法使用。

  1. unity工程设置:

    image.png

  2. 使用:
    如果工程merge出现冲突,

    image.png

  • 输入git mergetool, 就会自动调用YAMLMerge。自动修复场景和prefab文件冲突。
    image.png
  • 当遇到不能解决的冲突时,将自动打开你定义的fallback merge tool,然后你自己决定保留哪个就行了。
  • 冲突解决完毕后,git add --all or git add . 提交修改,然后 git rebase --continue(rebase) 或 git merge --continue继续合并。
    image.png

作者:Angeladaddy