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)

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

  1. Add * text=auto to the top of the .gitattributes file.

  2. For every binary file extension ext, add *.ext binary. For example, *.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 and OpenPGP (default). In this example, the key ID is 3AB5C344325645B2 and the key format is X.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 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 = "C:\Users\username\.ssh\id_ed25519";
    git.PublicKeyFile.PersonalValue = "C:\Users\username\.ssh\id_ed25519.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 using the key you define in the settings.

  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 = "~/.ssh/id_ed25519";
    git.PublicKeyFile.PersonalValue = "~/.ssh/id_ed25519.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 using the key you define in the settings.

  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.

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:

  1. Download and run the Git for Windows installer using the instructions described in Install Command-Line Git Client.

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

  2. In the Select Components sections, make sure you select Git LFS and Associate .sh file to be run with Bash. This enables you to use Git LFS and Git hooks on Windows.

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

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

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

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.

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

  5. Restart MATLAB.

See Also

Functions

Related Topics