Main Content

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, 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 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:

  1. Download and run the installer from https://www.cygwin.com/.

  2. In MATLAB, open librarypath.txt.

    edit(fullfile(matlabroot,"toolbox","local","librarypath.txt"))

  3. 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.

  4. 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 SystemInstructions
Windows
  1. Install a command-line Git client and make it available system-wide. 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. For more information, see Install Command-Line Git Client.

  2. Close all MATLAB sessions.

  3. Generate an SSH key using the ssh-keygen command. For example, at a Windows 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 passphrase. If you do not want to type a password when you use the key, leave the passphrase empty.

    If you already have keys in the specified folder, ssh-keygen asks if you want to override them.

    Before R2021a, specify the -m PEM option to generate an SSH key in the RSA format. Otherwise, ssh-keygen creates the SSH key using the default OpenSSH format, which is not supported in MATLAB versions before R2021a. For example, at a Windows command prompt, enter this command:

    ssh-keygen -m PEM
    If you generate an SSH key without specifying the -m PEM option, you can convert your key to the supported RSA format using this command, where ~/.ssh/id_rsa is the name of the SSH key file.
    ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

  4. Place your keys in the USERPROFILE/.ssh or HOME/.ssh folder. To verify which USERPROFILE folder MATLAB is working with, in the MATLAB Command Window, enter:

    getenv('USERPROFILE')

    Before R2021a, if you created the SSH key using the -m PEM option, you can skip step 5.

  5. To make sure that MATLAB detects the SSH keys, in the MATLAB Command Window, enter:

    git = settings().matlab.sourcecontrol.git;
    git.PrivateKeyFile.PersonalValue = '/path/to/key';
    git.PublicKeyFile.PersonalValue = '/path/to/key.pub';

    If you entered a passphrase when generating the SSH key, configure MATLAB to use the passphrase and receive a prompt only once per session. To do so, use Pageant or, in the MATLAB Command Window, enter:

    git.KeyHasPassphrase.PersonalValue = true;

    To use multiple keys, use Pageant as the SSH agent. If Pageant is running, MATLAB looks for keys in Pageant before looking in USERPROFILE/.ssh.

  6. 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 the SSH keys section, paste the contents of the .pub file into the Add SSH key field.

Linux® and macOS
  1. Close all MATLAB sessions.

  2. Generate an SSH key using the ssh-keygen command. For example, in a Terminal window, 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 passphrase. If you do not want to type a password when you use the key, leave the passphrase empty.

    If you already have keys in the specified folder, ssh-keygen asks if you want to override them.

    Before R2021a, specify the -m PEM option to generate an SSH key in the RSA format. Otherwise, ssh-keygen creates the SSH key using the default OpenSSH format, which is not supported in MATLAB versions before R2021a. For example, in a Terminal window, enter this command:

    ssh-keygen -m PEM
    If you generate an SSH key without specifying the -m PEM option, you can convert your key to the supported RSA format using this command, where ~/.ssh/id_rsa is the name of the SSH key file.
    ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

  3. Place your keys in the HOME/.ssh folder. To verify which HOME folder MATLAB is working with, in the MATLAB Command Window, enter:

    getenv('HOME')

    Before R2021a, if you created the SSH key using the -m PEM option, you can skip step 4.

  4. To make sure that MATLAB detects the SSH keys, in the MATLAB Command Window, enter:

    git = settings().matlab.sourcecontrol.git;
    git.PrivateKeyFile.PersonalValue = '/path/to/key';
    git.PublicKeyFile.PersonalValue = '/path/to/key.pub';

    If you entered a passphrase when generating the SSH key, configure MATLAB to use the passphrase and receive a prompt only once per session. To do so, use an SSH agent or, in the MATLAB Command Window, enter:

    git.KeyHasPassphrase.PersonalValue = true;

    To use multiple keys, use an SSH agent. If an SSH agent is running, MATLAB looks for keys in the agent before looking in HOME/.ssh.

  5. 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 the SSH keys section, paste the contents of the .pub file into the Add SSH key field.

Install Git Credential Helper

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 SystemInstructions
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.

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 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.

Note

Git LFS uses Git hooks. Make sure that you have Cygwin installed. For more information, see Configure MATLAB for Git on Windows.

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 SystemInstructions
Windows
  1. Download and run the installer from https://gitforwindows.org/.

  2. 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.

  3. 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.

  4. 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:

sudo apt-get install git
macOSOn 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.

Related Topics