Main Content

Set Up Git Source Control

Since R2025a. Replaces Set Up Git Source Control (R2024b).

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:

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, and post-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.

  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

Enable support for long paths on a Windows system by following these steps.

  1. On the Home tab, in the Environment section, click Settings. Select MATLAB > Source Control > Git.

  2. In the Windows section, select Enable support for long paths. Doing so sets the value of core.longpaths to true 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.

  1. On the Home tab, in the Environment section, click Settings. Select MATLAB > Source Control > Git.

  2. In the Commit Signing section, set the signing key, the signing key format, and the signing program. For more information, see Configure 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.

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

  2. Set up the use of SSH keys in MATLAB. On the Home tab, in the Environment section, click Settings. Select MATLAB > Source Control > Git. Then, configure your settings.

    1. In the SSH section, if not enabled, select Enable 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.

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

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

By default, MATLAB remembers your user names and tokens when you interact with Git repositories. To change the default credentials preference, on the Home tab, in the Environment section, click Settings. Select MATLAB > Source Control > Git. 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

  • 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

See Also

Functions

Topics