Set UpGit 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, seeTrack Work Locally with Git in MATLAB andCollaborate 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, seeRegister Binary Files with Git.
(Recommended for Windows® systems) Enable support for long paths for Git on a Windows system. For more information, seeEnable 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, seeEnable Signing Commits.
(Optional) Configure MATLAB to use Git SSH authentication or install a Git credential helper to prevent frequent login prompts. For more information, seeConfigure MATLAB to Use Git SSH Authentication.
(Optional) Configure MATLAB to manage Git credentials. For more information, seeManage 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, seeCustomize External Source Control to Use MATLAB for Diff and Merge.
Starting in R2024a, you can run Git hooks from within MATLAB. Supported hooks arepre-commit,commit-msg,post-commit,prepare-commit-msg,pre-push,pre-merge-commit,post-checkout, andpost-merge. For an example, seeUse Git Hooks in MATLAB.
Register Binary Files withGit
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=autoto the top of the.gitattributesfile.For every binary file extension
ext, add*.. For example,extbinary*.mlapp binary.
Enable Support for Long Paths
Enable support for long paths on a Windows system by following these steps.
On theHome tab, in theEnvironment section, clickSettings. SelectMATLAB >Source Control >Git.
In theWindows section, selectEnable support for long paths. Doing so sets the value of
core.longpathstotruein your global Git configuration file.
For more information, seeConfigure 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 theBranch Manager and the line annotations in the MATLAB Editor.
On theHome tab, in theEnvironment section, clickSettings. SelectMATLAB >Source Control >Git.
In theCommit Signing section, set the signing key, the signing key format, and the signing program. For more information, seeConfigure Git Settings.
ConfigureMATLAB to UseGit 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-keygencommand. For example, at a command prompt, enter this command:ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygenprompts 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-keygenasks if you want to override them.Set up the use of SSH keys in MATLAB. On theHome tab, in theEnvironment section, clickSettings. SelectMATLAB >Source Control >Git. Then, configure your settings.
In theSSH 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, seeKey-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 theUse SSH agent checkbox. Then, specify the path to your public and private key files inPublic key file andPrivate key file. For more information, seeConfigure Git Settings.
To enable the use of a pass-phrase and receive a prompt once per session, selectKey is pass-phrase protected.
Configure your GitHub® or GitLab® account to use the SSH keys. To do so, go to the
.sshfolder and copy the contents of the.pubfile. Then, go to your account settings, and in theSSH keys section, paste the contents of the.pubfile into theAdd SSH key field.
ManageGit 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 theEnvironment section, clickSettings. SelectMATLAB >Source Control >Git. In theCredentials section, choose one of the available options. For more information, seeConfigure 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 inAdditional Setup.
To delete saved login information for a Git repository, in MATLAB, enter:
matlab.git.clearCredential("https://github.com/myrepo.git")ConfigureGit to UseGit 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 inAdditional Setup. In theSelect Components section of the Git for Windows installer, select theGit LFS (Large File Support) andAssociate .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 ClientTo check if Git is installed, enter the command
!gitin 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 fromhttps://gitforwindows.org/.
In theSelect Components sections, make sure you selectGit LFS andAssociate .sh file to be run with Bash.
SelectingGit LFS enables you to use Git LFS on Windows.
SelectingAssociate .sh file to be run with Bash enables you to use Git hooks on Windows. For an example on how to use Git hooks, seeUse 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 run
mlAutoMerge.baton Windows. SeeAutomatically Merge Models Locally and in CI Pipeline (Simulink)
In theAdjusting your PATH environment section, choose the install optionGit from the command line and also from 3rd-party software. This option adds Git to your
PATHvariable and makes it available system-wide so that MATLAB can communicate with Git.In the section for configuring the line-ending conversions, choose theCheckout 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
.gitattributesfile 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.
ConfigureMATLAB to UseGit Credential HelperTo 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 theChoose a credential helper section of the installer, selectGit Credential Manager Core as the credential helper. This defines the value of
credential.helperin your global.gitconfigfile.Alternatively, you can set the value of
credential.helperin your global.gitconfigfile manually.
See Also
Functions
Topics
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)