Merging Simulink Models Automatically in Git
This post is targeted at users developing Simulink models under Git source control, but who are not necessarily using the MATLAB source control integration.
I personally work in MATLAB and Simulink all day, so the MATLAB source control integration usually does the job for me. However, I realize that many Simulink users deal with projects including multiple software and artifacts, and prefer to use Git command-line or a dedicated Git client to manage all that. Examples I see regularly are GitHub Desktop, GitKraken and Sourcetree.
If that's your case, keep reading. This post goes step-by-step through the instructions provided in this documentation page: Automatically Merge Models Locally and in CI Pipeline - MATLAB & Simulink
The Problem
Let's use a very simple example. I have a folder with a Simulink model

I initialize the folder for Git, add and commit the model:
git init
git add simpleMdl.slx
git commit -a -m "Initial commit"
I then create a separate branch where I make a modification in Subsystem A and commit the changes.
git checkout -b devA

git commit -a -m "Added Gain block in A"
At the same time, a second user create another branch and makes modifications to subsystem B
git checkout Master
git checkout -b devB

git commit -a -m "Added Bias block in B"
In the meantime, unrelated changes have been committed to the Master branch, so I have a situation like this:

Now I want to merge branches devA and devB into the master branch
git checkout Master
git merge devA
git merge devB
At this point, I will get an error that looks like this:
warning: Cannot merge binary files: simpleMdl.slx (HEAD vs. devB)
Auto-merging simpleMdl.slx
CONFLICT (content): Merge conflict in simpleMdl.slx
Automatic merge failed; fix conflicts and then commit the result.
As the warning says, since Simulink models are binary files, by default Git cannot merge automatically.
Let's see how to fix that!
Configure the global .gitconfig
For many releases now, MathWorks ships with Simulink diff and merge tools that can be registered in Git. Those need to be registered in your global .gitconfig file.
To make it easy, you can simply type this line in MATLAB:
comparisons.ExternalSCMLink.setupGitConfig();
This will automatically add the required lines to your global .gitconfig file.
Configure your repository
The next step is to add a .gitattributes file to your repository to tell Git that SLX Simulink models are binary files and can be merged using the previously registered tools.
*.slx binary merge=mlAutoMerge
*.mdl binary merge=mlAutoMerge
Note that when using the MATLAB source control integration, this .gitattributes file is automatically generated and added to your repository. If you prefer managing your source control business outside of MATLAB, you need to do it yourself.
Let's Merge!
With that setup, Git will now be able to automatically merge Simulink models. Here is a screenshot where I use Git Bash to do the merge that had previously failed:

I can look at the model to confirm that all the changes have been merged properly.

What if there is a conflict or we cannot auto-merge?
If you took the time to read Automatically Merge Models Locally and in CI Pipeline - MATLAB & Simulink, you hopefully noticed this note:

For example, let's say I have a library where a different block has been added at the top level in two branches:

The auto-merge will fail when trying to merge branch B after having merged branch A.
$ git merge devB
Auto-merging simpleMdl.slx
CONFLICT (content): Merge conflict in simpleMdl.slx
Automatic merge failed; fix conflicts and then commit the result.
In that case, you will need to launch MATLAB. In R2025a, you can use the new Source Control panel and select View Conflicts:

This will launch the Three-Way Merge tool. In this simple case, the tool will be able to automatically do the merge, so all you have to do is click Apply & Close.

The changes have been merged and are now ready to commit.

Now it's your turn
Are you having success merging Simulink models? Let us know in the comments below.
- Category:
- Commands,
- Model-Based Design
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.