Main Content

Set Up Git Source Control

To use the version of Git™ provided with projects, when you add a project to source control or retrieve from source control, select Git in the Source control tool list.

  • If you add an existing project to Git source control, you create a local Git repository in that sandbox. You can specify a remote repository later. See Add a Project to Source Control.

  • If you want to clone a remote Git repository to create a project, on the MATLAB® Home tab, select New > Project > From Git. After you specify a remote repository to clone, a local repository is created. You can also pull, fetch, and push changes to and from the remote repository. See Clone Git Repository.

    To use a Git server for your remote repository, you can use a Git server hosting solution or set up your own Apache™ Git server. If you cannot set up a server and must use a remote repository via the file system using the file:/// protocol, make sure it is a bare repository with no working copy checked out.

  • To make your project publicly available on GitHub®, see Share Project on GitHub. Sharing adds Git source control to the open project and the project’s remote repository is GitHub.

Configure MATLAB on Windows

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/simulink/ug/set-up-git-source-control.html.

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, see Locate Native Method Libraries.

  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, configure a Git credential manager to remember credentials or add a new public key and clone the repository using SSH instead. 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 Model Files with Git.

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

!git config --global core.longpaths true

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 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. On Linux and Mac, 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')

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

    On Windows, place your keys in the USERPROFILE/.ssh or HOME/.ssh folder. To verify which USERPROFILE directory the MATLAB Git integration is working with, in the MATLAB Command Window, enter:

    getenv('USERPROFILE')

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

  3. 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 Model Files with Git

You can prevent Git from corrupting your Simulink® models 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.

Configure Git Credential Helper

You can configure Git to use a credential helper to remember usernames and passwords. For all platforms, Git Credential Manager Core is recommended as the credential helper.

To remember credentials, Git must be installed. On Windows, install Git for Windows.

Related Topics