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)
Enable support for long paths for Git on a Windows® system. (Recommended for Windows Systems)
Configure MATLAB to sign commits. (Optional)
Configure MATLAB to use Git SSH authentication or install a Git credential helper to prevent frequent login prompts. (Optional)
Configure Git to use Git LFS if you are working with large files. (Optional)
Configure external Git tools to use MATLAB for Diff and Merge. (Optional)
Starting in R2024a, MATLAB
Git integration runs Git hooks with no additional setup. Supported hooks are
pre-commit
, commit-msg
,
post-commit
, prepare-commit-msg
,
pre-push
, pre-merge-commit
,
post-checkout
, and post-merge
. For an example, see
Use Git Hooks in MATLAB.
Before R2024a, to use some Git hooks with MATLAB, install Cygwin™ and add it to the MATLAB library path. For instructions, see Install Cygwin (Before R2024a).
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 the .gitattributes
file in your
repository. 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, create a blank .gitattributes
file in your
repository and populate its content.
Add
* text=auto
to the top of the.gitattributes
file.For every binary file extension
ext
, add*.
. For example,ext
binary*.mlapp binary
.
Enable Support for Long Paths
To enable support for long paths on a Windows system, run this command in MATLAB:
!git config --global core.longpaths true
Enable Signing Commits
To sign commits, configure Git by running these commands in MATLAB.
Enable signing commits automatically.
!git config --global commit.gpgsign true
Set your signing key and the key format. Supported key formats are
X.509
andOpenPGP
(default). In this example, the key ID is3AB5C344325645B2
and the key format isX.509
.!git config --global user.signingkey 3AB5C344325645B2 !git config --global gpg.format x509
Specify the utility used to sign commits. In this example, the command configures Git to use
smimesign
as a signing utility.!git config --global gpg.x509.program smimesign
To use
OpenPGP
instead, run!git config --global gpg.openpgp.program gpg
.
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 |
|
Manage Git Credentials
MATLAB remembers your user names and tokens when you interact with Git repositories.
Alternatively, you can install an external Git credential helper and configure MATLAB to use it instead. The recommended credential helper for all platforms is Git Credential Manager Core. For more information, see Configure MATLAB to Use Git Credential Helper.
To delete saved login information for a Git repository, in MATLAB, enter:
matlab.git.clearCredential("https://github.com/myrepo.git")
Configure MATLAB to Use Git Credential Helper
To install Git Credential Manager Core on a Windows system and configure MATLAB to use it to store Git credentials, follow these steps:
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. This defines the value of
credential.helper
in your global.gitconfig
file.Alternatively, you can set the value of
credential.helper
in your global.gitconfig
file manually.
Disable Compression for Simulink Models (Before R2023b)
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
Starting 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 External Git Tools 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 External Git tools to use the MATLAB Comparison Tool, see Customize External Source Control to Use MATLAB for Diff and Merge.
Additional Setup
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. |
Install Cygwin (Before R2024a)
Before R2024a, to run some Git hooks from within MATLAB, you must install Cygwin. Supported hooks are pre-commit
,
commit-msg
, post-commit
, and
pre-push
.
Follow these steps to install Cygwin on a Windows system and add it to the MATLAB library path and the system 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.Add the Cygwin bin folder to the system path. For more information, see https://www.mathworks.com/matlabcentral/answers/94933-how-do-i-edit-my-system-path-in-windows.
Restart MATLAB.