Set Up Git Source Control
You can use Git™ source control in MATLAB® to manage your files and collaborate with others. Using Git, you can track changes to your files and recall specific versions later. For more information, see Clone Git Repository and Add a Project to Source Control.
Before using Git, follow these steps to set it up for MATLAB:
Register your binary files with Git to avoid file corruption. (Required for all systems)
Configure MATLAB for Git on a Windows® system. (Recommended for Windows Systems)
Configure MATLAB to use Git SSH authentication or install a Git credential helper to prevent frequent login prompts. (Optional)
Disable compression for Simulink® models to reduce your Git repository size. (Optional)
Configure Git to use Git LFS if you are working with large files. (Optional)
Configure Git to use MATLAB for Diff and Merge. (Optional)
Before R2020b, a command-line Git client must be installed to use Git to merge branches in MATLAB. For more information, see Install Command-Line Git Client.
Register Binary Files with Git
If you use third-party source control tools, you must register your MATLAB and Simulink file extensions such as .mlx
, .mat
,
.fig
, .mlapp
, .mdl
,
.slx
, .mdlp
, .slxp
,
.sldd
, and .p
as binary formats. Also register
extensions for MEX files, such as .mexa64
, .mexmaci64
,
.mexmaca64
, and .mexw64
. If you do not register
the extensions, these tools can corrupt your files when you submit them by changing
end-of-line characters, expanding tokens, substituting keywords, or attempting to automerge.
Corruption can occur if you use the source control tools outside of MATLAB or if you try submitting files from MATLAB without first registering your file formats.
Also check that other file extensions are registered as binary to avoid corruption at
check-in. Check and register file extensions such as .xlsx
,
.jpg
, .pdf
, .docx
, and so
on.
To register your binary file extensions with Git, add them to a .gitattributes
file. If you create a new
project that uses Git source control or switch an existing project from another source control
system to Git source control, MATLAB automatically creates a .gitattributes
file and
populates it with a list of common binary files to register.
If a .gitattributes
file is not automatically created, you can
create one that contains the list of common binary files to register. In the MATLAB Command Window,
enter:
copyfile(fullfile(matlabroot,'toolbox','shared','cmlink','git','auxiliary_files', ... 'mwgitattributes'),fullfile(pwd,'.gitattributes'))
Alternatively, to create a blank .gitattributes
file,
enter:
edit .gitattributes
To register binary files that are not already included in the
.gitattributes
file, add a line to the file for each file type
that you need. For example, *.mlapp binary
.
Configure MATLAB for Git on Windows
On Windows systems, if you are working with files that have long paths, enable support for long paths. In addition, to use some Git hooks with MATLAB, install Cygwin and add it to the MATLAB library path. Git hooks are custom scripts that can be triggered by operations such as committing, merging, and receiving pushed commits.
Enable Support for Long Paths (Recommended)
To enable support for long paths on a Windows system, run this command in MATLAB:
!git config --global core.longpaths true
Install Cygwin (Optional)
To install Cygwin on a Windows system and add it to the MATLAB library path:
Download and run the installer from https://www.cygwin.com/.
In MATLAB, open
librarypath.txt
.edit(fullfile(matlabroot,"toolbox","local","librarypath.txt"))
Add the Cygwin bin folder location to the end of
librarypath.txt
, for example,C:\cygwin64\bin
.## ## FILE: librarypath.txt ## ## Entries: ## o path_to_jnifile ## o [alpha,glnx86,sol2,unix,win32,mac]=path_to_jnifile ## o $matlabroot/path_to_jnifile ## o $jre_home/path_to_jnifile ## $matlabroot/bin/$arch $matlabroot/sys/jxbrowser/$arch/lib C:\cygwin64\bin
If you do not have permission to edit the
librarypath.txt
file, see Locate Native Method Libraries.Restart MATLAB.
Configure MATLAB to Use Git SSH Authentication
To prevent frequent login prompts when you interact with your remote repository using HTTPS, add a new public key and clone the repository using SSH instead. This table provides instructions on how to configure MATLAB to use SSH authentication based on your operating system.
Operating System | Instructions |
---|---|
Windows |
|
Linux® and macOS |
|
Install Git Credential Helper
Since R2021a
To configure MATLAB to remember user names and passwords when using Git, install a credential helper. The recommended credential helper for all platforms is Git Credential Manager Core.
For example, to install Git Credential Manager Core on a Windows system, download and run the Git for Windows installer using the instructions described in Install Command-Line Git Client. In the Choose a credential helper section of the installer, select Git Credential Manager Core as the credential helper.
To delete saved login information for Git repositories, follow instructions based on your operating system.
Operating System | Instructions |
---|---|
Windows | To reset the Git credentials for your repository, in the Credential Manager, in the Windows Credentials tab, delete the entries under Generic Credentials. |
Linux and macOS | To reset the Git credentials for your repository, enter: echo "url=https://github.com/myrepo.git" | git credential reject |
Disable Compression for Simulink Models
You can reduce your Git repository size by saving Simulink models without compression. Turning off compression results in larger SLX files on disk but reduces repository size.
To use this setting with new SLX files, create your models using a model template with SLX Compression set to none. For existing SLX files, set the compression and then save the model. For more information, see Set SLX Compression Level.
Note
Staring in R2023b, by default, Simulink applies no compression during the save operation.
Configure Git to Use Git LFS
Since R2021a
If you are working with large files, configure Git to use Git Large File Storage (LFS) by installing command-line Git and setting up LFS.
For example, to use Git LFS on a Windows system, download and run the Git for Windows installer using the instructions described in Install Command-Line Git Client. In the Select Components section of the Git for Windows installer, select the Git LFS (Large File Support) and Associate .sh files to be run with Bash options.
MATLAB does not support Git LFS locking. In addition, MATLAB does not support LFS commands such as git lfs track
.
Use !git lfs track
instead.
Configure Git to use MATLAB for Diff and Merge
You can configure Git to use the MATLAB Comparison Tool for diff and merge. The MATLAB Comparison Tool provides tools for merging MathWorks® files such as live scripts, MAT, SLX, or MDL files. You can use the automerge tool with Git to automatically merge branches that contain changes in different subsystems in the same SLX file.
For more information about how to configure Git to use the MATLAB Comparison Tool, see Customize External Source Control to Use MATLAB for Diff and Merge.
Install Command-Line Git Client
To use Git LFS or a credential helper, you must install a command-line Git client and make it available system-wide. Before R2020b, a command-line Git client must be installed to use Git to merge branches in MATLAB.
To check if Git is installed, enter the command !git
in the MATLAB Command Window. If the command does not return anything, you need to
install command-line Git. This table provides instructions on how to install command-line
Git based on your operating system.
Operating System | Instructions |
---|---|
Windows |
|
Linux | Git is available for most distributions. Install Git for your distribution. For example, on Debian®, install Git by entering: sudo apt-get install git |
macOS | On Mavericks (10.9) or above, run Git from a Terminal window. If you do not have Git installed already, it will prompt you to install Xcode Command Line Tools. For more information, see https://git-scm.com/doc. |