Set Up Git Source Control - MATLAB & Simulink (original) (raw)
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 Track Work Locally with Git in MATLAB and Collaborate Using Git in MATLAB.
Before using Git, follow these steps to set it up for MATLAB:
- (Required for all systems) Register your binary files with Git to avoid file corruption. For more information, see Register Binary Files with Git.
- (Recommended for Windows® systems) Enable support for long paths for Git on a Windows system. For more information, see Enable Support for Long Paths.
- (Recommended for all systems) Configure MATLAB to automatically merge model files. For more information, seeAutomatically Merge Models Locally and in CI Pipeline (Simulink).
- (Optional) Configure MATLAB to sign commits. For more information, see Enable Signing Commits.
- (Optional) Configure MATLAB to use Git SSH authentication or install a Git credential helper to prevent frequent login prompts. For more information, see Configure MATLAB to Use Git SSH Authentication.
- (Optional) Configure MATLAB to manage Git credentials. For more information, see Manage Git Credentials.
- (Optional) Configure Git to use Git LFS if you are working with large files. For more information, seeConfigure Git to Use Git LFS.
- (Optional) Configure external Git tools to use MATLAB for Diff and Merge. For more information, see Customize External Source Control to Use MATLAB for Diff and Merge.
Starting in R2024a, you can run Git hooks from within MATLAB. Supported hooks are pre-commit
,commit-msg
, post-commit
,prepare-commit-msg
, pre-push
,pre-merge-commit
, post-checkout
, andpost-merge
. For an example, see Use Git Hooks in MATLAB.
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 register other file extensions, such as .xlsx
,.jpg
, .pdf
, and .docx
, as binary formats to avoid corruption during the check-in operation.
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*._`ext`_ binary
. For example,*.mlapp binary
.
Enable Support for Long Paths
Enable support for long paths on a Windows system by following these steps.
- On the Home tab, in theEnvironment section, clickSettings. Select > > .
- In the Windows section, select Enable support for long paths. Doing so sets the value of
core.longpaths
totrue
in your global Git configuration file.
For more information, see Configure Git Settings.
Enable Signing Commits
Configure your source control settings to enable MATLAB to sign Git commits automatically by following these steps. When MATLAB verifies a commit signature, a green verification icon appears next to your avatar and username in both the Branch Manager and the line annotations in the MATLAB Editor.
- On the Home tab, in theEnvironment section, clickSettings. Select > > .
- In the Commit Signing section, set the signing key, the signing key format, and the signing program. For more information, seeConfigure Git Settings.
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. Configure MATLAB to use SSH authentication by following these steps.
- Generate an SSH key using the
ssh-keygen
command. For example, at a command prompt, enter this command:
ssh-keygen -t ed25519 -C "your_email@example.com"ssh-keygen
prompts you to confirm where to save the key and asks for a pass-phrase. If you do not want to type a password when you use the key, leave the pass-phrase empty.
If you already have keys in the specified folder,ssh-keygen
asks if you want to override them. - Set up the use of SSH keys in MATLAB. On the Home tab, in theEnvironment section, clickSettings. Select > > . Then, configure your settings.
- In the SSH section, if not enabled, selectEnable SSH.
By default, MATLAB looks for keys in an SSH agent. On Windows, use OpenSSH as the SSH agent. For more information, see Key-based authentication in OpenSSH for Windows.
Using an SSH agent enables you to store and use multiple keys. - If you are not using an SSH agent to store your keys, you can enter them manually. Clear the Use SSH agent checkbox. Then, specify the path to your public and private key files in Public key file and Private key file. For more information, see Configure Git Settings.
To enable the use of a pass-phrase and receive a prompt once per session, select Key is pass-phrase protected.
- In the SSH section, if not enabled, selectEnable SSH.
- Configure your GitHub® or GitLab® account to use the SSH keys. To do so, go to the
.ssh
folder and copy the contents of the.pub
file. Then, go to your account settings, and in theSSH keys section, paste the contents of the.pub
file into the Add SSH key field.
Manage Git Credentials
By default, MATLAB remembers your user names and tokens when you interact with Git repositories. To change the default credentials preference, on theHome tab, in the Environment section, click Settings. Select > > . In the Credentials section, choose one of the available options. For more information, see Configure Git Settings.
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 in Additional Setup.
To delete saved login information for a Git repository, in MATLAB, enter:
matlab.git.clearCredential("https://github.com/myrepo.git")
Configure Git to Use Git LFS
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 under Install Command-Line Git Client in Additional Setup. 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.
Additional Setup
- To use Git LFS or a credential helper, you must install a command-line Git client and make it available system-wide.
Install Command-Line Git Client
To check if Git is installed, enter the command!git
in the MATLAB Command Window. If the command does not return an output, you need to install a command-line Git client. This table provides instructions on how to install a command-line Git client based on your operating system.Operating System Instructions Windows Download and run the installer from https://gitforwindows.org/.In the Select Components sections, make sure you select Git LFS and Associate .sh file to be run with Bash. Selecting Git LFS enables you to use Git LFS on Windows.Selecting Associate .sh file to be run with Bash enables you to use Git hooks on Windows. For an example on how to use Git hooks, see Use Git Hooks in MATLAB.By default, Git command-line installation includes Git bash that provides shell utilities. When you configure MATLAB to automatically merge Simulink models, Git bash enables you to runmlAutoMerge.bat on Windows. SeeAutomatically Merge Models Locally and in CI Pipeline (Simulink)In the Adjusting your PATH environment section, choose the install option Git from the command line and also from 3rd-party software. This option adds Git to your PATH variable and makes it available system-wide so that MATLAB can communicate with Git.In the section for configuring the line-ending conversions, choose the Checkout Windows-style, commit Unix-style line endings option. The line-ending format is not enforceable between machines and users, but you can support consistent line endings for text files in the .gitattributes file of each repository.Restart your system for the changes to take effect. Linux® Git is available for most distributions. Install Git for your distribution. For example, on Debian®, install Git by entering this command: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, seehttps://git-scm.com/doc. - MATLAB remembers your usernames and tokens when you interact with Git repositories. You can use an external Git credential helper to store your Git credentials instead. The Git Credential Manager Core is the recommended credential helper for all platforms.
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 the above section.
- 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 ofcredential.helper
in your global.gitconfig
file manually.