Set Up Git Source Control

Note

Starting in R2020b, you do not need to install command-line Git™ to fully use Git with MATLAB®. You can now merge branches using the built-in Git integration.

To setup Git for releases before R2020b, see https://www.mathworks.com/help/releases/R2020a/matlab/matlab_prog/set-up-git-source-control.html.

Configure MATLAB on Windows

Several operations, such as committing, merging, and receiving pushed commits, use Git Hooks. To use Git Hooks on Windows® with MATLAB, install Cygwin and add it to the MATLAB library path:

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

  2. In the MATLAB Command Window, type edit(fullfile(matlabroot,"toolbox","local","librarypath.txt")).

    Add the Cygwin bin folder location to the end of librarypath.txt, for example, C:\cygwin64\bin.

    If you do not have permission to edit the librarypath.txt file, create a copy and save it to your MATLAB start folder.

  3. Restart MATLAB for the changes to take effect.

You can clone a remote repository like GitHub® and GitLab™ using HTTPS or SSH. 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. To avoid problems connecting using SSH, set the HOME environment variable and use it to store your SSH keys. For more information, see Use SSH Authentication with MATLAB.

For new projects under Git source control, MATLAB automatically registers your binary files to prevent corruption when merging. For existing projects, register the binary files before using Git to merge branches. For more information, see Register Binary Files with Git.

If you are working with long path files, run this command in MATLAB:

!git config --global core.longpaths true

Use SSH Authentication with MATLAB

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.

MATLAB Git integration uses the user HOME environment variable to locate the .ssh folder containing SSH keys. If the HOME environment variable is not set or the SSH keys are not stored properly, you will encounter problems using SSH to connect to remote repositories like GitHub and GitLab.

To use SSH authentication inside MATLAB:

  1. Use ssh-keygen to generate valid SSH keys. In the Command Prompt, enter:

    ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (C:\Users\username/.ssh/id_rsa):
    Created directory 'C:\Users\username/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in C:\Users\username/.ssh/id_rsa.
    Your public key has been saved in C:\Users\username/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:/Nc9/tnZ7Dmh77+iJMxmPVrlPqaFd6J1j1YRXEk3Tgs company\username@us-username
    ssh-keygen confirms where to save the key (for example, .ssh/id_rsa) 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.

    Note

    It is not possible to generate SSH keys directly in MATLAB. Generate SSH keys using the ssh-keygen provided with a command-line Git install.

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

    getenv('HOME')
  3. If getenv('HOME') returns nothing, you need to set your HOME environment variable.

    To set the HOME environment variable in Windows:

    • In the Start Search box, search for and select "advanced system settings".

    • On the Advanced tab, click Environment Variables.

    • In the User Variables section, click New. Create the HOME environment variable and specify its value.

    The HOME environment variable is always set on Linux® and Mac.

  4. Configure your GitHub or GitLab account to use the SSH keys:

    • Copy the contents of .pub file in the .ssh folder.

    • Paste the contents in the Add SSH key field in the SSH keys section of your account settings.

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. 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 whether 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 files such as MEX-files, .xlsx, .jpg, .pdf, .docx, etc.

You can prevent Git from corrupting your files by registering binary files in your .gitattributes file.

  • For new projects and projects that switched from another source control system, MATLAB automatically creates a .gitattributes file and populates it with a list of binary files to register. This specifies that Git should not make automatic line feed, diff, and merge attempts for registered files.

  • For existing projects, create a .gitattributes file and populate it with the list of binary files to register.

    1. In the Command Window, type:

      edit .gitattributes

    2. Add a line to the attributes file for each file type you need. For example, *.mlapp binary.

      Tip

      You can copy a .gitattributes file that contains the list of common binary files to register.

      copyfile(fullfile(matlabroot,'toolbox','shared','cmlink','git','auxiliary_files','mwgitattributes'),fullfile(pwd,'.gitattributes'))

    3. Restart MATLAB so you can start using the Git client.

Tip

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 compression and then save the model. For more information, see Set SLX Compression Level (Simulink).

Related Topics