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.
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/. Run the installer.
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.
Restart MATLAB for the changes to take effect.
To use Git LFS or a credential helper, you must also install command-line Git. For more information, see Use Git LFS with MATLAB and Configure Git Credential Helper.
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 Configure Git Credential Helper and 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 trueTo 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:
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.
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.
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.
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.
In the Command Window, type:
edit .gitattributes
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'))
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.
You can configure MATLAB Git integration to use a credential helper to remember usernames and passwords. For all platforms, Git Credential Manager Core is recommended as the credential helper.
To use Credential Manager Core on Windows:
Download the installer from https://gitforwindows.org/ and run it.
In the section on choosing a credential helper, select Git Credential Manager Core.
To use Git Large File Storage (LFS) with MATLAB Git integration, install command-line Git and LFS.
To use Git LFS on Windows:
Download the installer from https://gitforwindows.org/ and run it.
In the section on choosing the components to install, select Git LFS (Large File Support) and Associate .sh files to be run with Bash.
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.
LFS uses Git Hooks. Make sure you have Cygwin installed. For more details, see Configure MATLAB on Windows.
MATLAB does not support Git LFS locking. MATLAB has no integration with LFS commands such as
git lfs track. Use !git lfs track
instead.