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.
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.
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 Binary 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:
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.
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.
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 (Simulink).
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.