Set Up Git Source Control

Use Git Source Control in Projects

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, select New > Project > From Git on the MATLAB® Home tab. After you specify a remote repository to retrieve from, a local repository is created. You can also pull, fetch, and push changes from and to the remote repository. See Clone Git Repository or Check Out SVN Repository.

    Note

    You cannot add empty folders to Git source control. Use Check Project instead. See Pull, Push, and Fetch Files with Git.

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

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

Install Command-Line Git Client and Configure MATLAB Installation

If you want to use Git to merge branches in MATLAB, you must install a command-line Git client and make it available system-wide.

Check if Git is installed using the command !git in MATLAB. If it returns nothing, you need to install command-line Git.

Operating SystemsInstructions
On Windows®

  1. Download the installer from https://gitforwindows.org/ and run it.

    • In the section on adjusting your PATH, 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.

    • In the section on choosing the SSH executable, choose the option Use OpenSSH. MATLAB only supports OpenSSH keys.

    • In the section on configuring the line-ending conversions, choose the option Checkout as-is, commit as-is to avoid converting any line endings in files.

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

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

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

    • Restart MATLAB for the changes to take effect.

  3. Restart your computer for the changes to take effect.

On 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

On Mac

On Mavericks (10.9) or above, run git from the Terminal. If you do not have Git installed already, it will prompt you to install Xcode Command Line Tools. For more options, see https://git-scm.com/doc.

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.

To avoid corrupting binary files, 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.

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 connecting using SSH 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 you want to save the key (.ssh/id_rsa for example) 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.

  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 on Windows:

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

    • In 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 Model Files with Git

After you install a command-line Git client, you can prevent Git from corrupting your Simulink® models by inserting conflict markers. To do so, edit your .gitattributes file to register model files as binary. For details, see:

  1. If you do not already have a .gitattributes file in your project root folder, create one by entering in MATLAB by entering:

    edit .gitattributes
    

  2. Add these lines to the .gitattributes file:

    *.mlx -crlf -diff -merge
    *.mat -crlf -diff -merge
    *.fig -crlf -diff -merge
    *.mdl -crlf -diff -merge
    *.slx -crlf -diff -merge
    *.mlapp -crlf -diff -merge
    *.p -crlf -diff -merge
    *.mdlp -crlf -diff -merge
    *.slxp -crlf -diff -merge
    *.sldd -crlf -diff -merge
    *.slxc -crlf -diff -merge
    *.mlproj -crlf -diff -merge
    *.mldatx -crlf -diff -merge
    *.slreqx -crlf -diff -merge
    *.sfx -crlf -diff -merge
    *.sltx -crlf -diff -merge
    These lines specify that Git should not attempt automatic line feed, diff, and merge attempts for MATLAB and Simulink files.

  3. Check for other file types you use in your projects that you need to register as binary. Check for files such as MEX-files (.mexa64, .mexmaci64, .mexw64), .xlsx, .jpg, .pdf, .docx, etc. Add a line to the attributes file for each file type you need.

    *.mexa64 -crlf -diff -merge
    *.mexw64 -crlf -diff -merge
    *.mexmaci64 -crlf -diff -merge
    *.xlsx -crlf -diff -merge
    *.docx -crlf -diff -merge
    *.pdf -crlf -diff -merge
    *.jpg -crlf -diff -merge
    *.png -crlf -diff -merge
  4. Restart MATLAB so you can start using the Git client with the project.

After you have installed a command-line Git client and registered your model files as binary, you can use the merging features of Git in projects.

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.

Add Git Submodules

To reuse code from another repository, you can specify Git submodules to include in your project.

To clone an external Git repository as a submodule:

  1. On the Project tab, in the Source Control section, click Submodules.

  2. In the Submodules dialog box, click the + button.

  3. In the Add Submodule dialog box, in the Remote box, specify a repository location. Optionally, click Validate.

  4. In the Path box, specify a location for the submodule in your project and click OK. The Submodules dialog box displays the status and details of the submodule.

  5. Check the status message, and click Close to return to your project.

Update Submodules

After using Pull on the top-level project, check submodules are up to date by clicking Submodules and then click Update. If any submodule definition have changed, then the update ensures that the submodule folder contains the correct files. Update applies to all child submodules in the submodule hierarchy.

Use Fetch and Merge with Submodules

When you want to manage a submodule, open the Submodules dialog box.

  1. To get the latest version of a submodule, in the Submodules dialog box, click Fetch.

  2. After fetching, you must merge. Check the Status message in the Submodules dialog box for information about your current branch relative to the remote tracking branch in the repository. When you see the message Behind, you need to merge in changes from the repository to your local branch.

  3. Click Branches and merge in the origin changes to your local branch using the Branches dialog box. See Pull, Fetch, and Merge.

Use Push to Send Changes to the Submodule Repository

If you make changes in your submodule and want to send changes back to the repository:

  1. Perform a local commit in the parent project.

  2. Open the Submodules dialog box and click Push.

If you want other project users to obtain your changes in the submodule when they clone the parent project, make sure the index and head match.

  1. In the Submodules dialog box, check the index and head values. The index points to the head commit at the time you first cloned the submodule, or when you last committed the parent project repository. If the index and head do not match, you must update the index.

  2. To update the index, commit your changes in the parent project, and then click Push in the Submodules dialog box. This action makes the index and head the same.

Related Topics