Main Content

设置GitSource Control

You can use Git™ source control in MATLAB®管理您的文件并与他人合作。使用git,您可以跟踪文件的更改并稍后再召回特定版本。有关更多信息,请参阅克隆git存储库andAdd a Project to Source Control

Before using Git, follow these steps to set it up for MATLAB:

  • Register your binary files with Git to avoid file corruption. (Required for all systems)

  • Configure MATLAB for Git on a Windows®系统。(为Windows系统推荐)

  • Configure MATLAB to use Git SSH authentication or install a Git credential helper to prevent frequent login prompts. (Optional)

  • Disable compression for Simulink®models to reduce your Git repository size. (Optional)

  • 如果您正在使用大文件,则配置Git以使用Git LFS。(可选的)

  • Configure Git to use MATLAB for Diff and Merge. (Optional)

Before R2020b, a command-line Git client must be installed to use Git to merge branches in MATLAB. For more information, see安装Command-Line Git Client

注册二进制文件Git

If you use third-party source control tools, you must register your MATLAB and Simulink file extensions such as。mlx,,,,。垫,,,,。fig,,,,.mlapp,,,,。mdl,,,,.slx,,,,.mdlp,,,,.slxp,,,,。sldd,,,,and.pas binary formats. Also register extensions for MEX files, such as。mexa64,,,,。mexmaci64,,,,and.mexw64。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 if 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 file extensions such as。xlsx,,,,。jpg,,,,.pdf,,,,。docx,,,,and so on.

To register your binary file extensions with Git, add them to a。gitattributesfile. If you create a new project that uses Git source control or switch an existing project from another source control system to Git source control, MATLAB automatically creates a。gitattributes文件并用要注册的常见二进制文件列表填充它。

如果一个。gitattributes文件不是自动创建的,您可以创建一个包含要注册的常见二进制文件列表。在MATLAB命令窗口中,输入:

copyfile(fullfile(matlabroot,,'toolbox',,,,'shared',,,,'cmlink',,,,“git”,,,,'auxiliary_files',,,,...'mwgitattributes'),fullfile(PWD,'.gitattributes'))

Alternatively, to create a blank。gitattributesfile, enter:

编辑。gitattributes

To register binary files that are not already included in the。gitattributes文件,为您需要的每个文件类型添加一行。例如,*.mlapp二进制

ConfigureMATLABforGitonWindows

On Windows systems, if you are working with files that have long paths, enable support for long paths. In addition, to use Git hooks with MATLAB, install Cygwin and add it to the MATLAB library path. Git hooks are custom scripts that can be triggered by operations such as committing, merging, and receiving pushed commits.

启用对长路径的金宝app支持(建议)

为了支持Window金宝apps系统上的长路径,请在MATLAB中运行此命令:

!git config -Global Core.LongPath true true

安装Cygwin(可选)

To install Cygwin on a Windows system and add it to the MATLAB library path:

  1. Download and run the installer fromhttps://www.cygwin.com/

  2. In MATLAB, openlibrarypath.txt

    编辑(fullfile(matlabroot,"toolbox",,,,"local",,,,"librarypath.txt"))

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

    ## ## file:librarypath.txt ## ##条目:## o path_to_jnifile ## o [alpha,glnx86,sol2,unix,win32,mac] = path_to_to_jnifile ## o $ matlabroot/path_to_to_to_to_to_to_jnifile ##path_to_jnifile ## $ matlabroot/bin/$ ach

    如果您没有权限编辑librarypath.txtfile, seeLocate Native Method Libraries

  4. Restart MATLAB.

ConfigureMATLAB使用GitSSH Authentication

为了防止使用HTTPS与远程存储库进行交互时频繁的登录提示,请添加新的公钥并使用SSH克隆存储库。该表提供了有关如何配置MATLAB以使用操作系统使用SSH身份验证的说明。

Operating System Instructions
Windows
  1. 安装命令行GIT客户端,并使其范围内可用。要检查是否安装了git,请输入命令!git在MATLAB命令窗口中。如果命令不返回任何内容,则需要安装命令行Git。有关更多信息,请参阅安装Command-Line Git Client

  2. Close all MATLAB sessions.

  3. 使用ssh-keygen命令。例如,在Windows命令提示符下,输入此命令:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    ssh-keygen提示您确认在哪里保存钥匙并要求使用密码。如果您不想在使用键时输入密码,请将密码留为空。

    If you already have keys in the specified folder,ssh-keygenasks if you want to override them.

    在R2021a之前,指定-m Pemoption to generate an SSH key in the RSA format. Otherwise,ssh-keygen使用默认的OpenSSH格式创建SSH键,该格式在R2021A之前不支持MATLAB版本。金宝app例如,在Windows命令提示符下,输入此命令:

    ssh-keygen -m PEM
    If you generate an SSH key without specifying the-m Pemoption, you can convert your key to the supported RSA format using this command, where〜/.ssh/id_rsa是SSH密钥文件的名称。
    ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

  4. 将钥匙放在USERPROFILE/.sshorHOME/.sshfolder. To verify whichUSERPROFILEfolder MATLAB is working with, in the MATLAB Command Window, enter:

    getenv('用户资料'

    Before R2021a, if you created the SSH key using the-m Pem选项,您可以跳过步骤5。

  5. To make sure that MATLAB detects the SSH keys, in the MATLAB Command Window, enter:

    git = settings().matlab.sourcecontrol.git; git.PrivateKeyFile.PersonalValue ='/path/to/key';git.PublicKeyFile.PersonalValue ='/path/to/key.pub';

    如果你进入了一个密码,当生成党卫军H key, configure MATLAB to use the passphrase and receive a prompt only once per session. To do so, use Pageant or, in the MATLAB Command Window, enter:

    git.KeyHasPassphrase.PersonalValue = true;

    To use multiple keys, use Pageant as the SSH agent. If Pageant is running, MATLAB looks for keys in Pageant before looking inUSERPROFILE/.ssh

  6. Configure your GitHub®或gitlab®帐户使用SSH键。为此,去。ssh文件夹并复制内容.pubfile. Then, go to your account settings, and in theSSH键section, paste the contents of the.pub归档到添加SSH键场地。

Linux®andmacOS
  1. Close all MATLAB sessions.

  2. 使用ssh-keygen命令。例如,在终端窗口中,输入此命令:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    ssh-keygen提示您确认在哪里保存钥匙并要求使用密码。如果您不想在使用键时输入密码,请将密码留为空。

    If you already have keys in the specified folder,ssh-keygenasks if you want to override them.

    在R2021a之前,指定-m Pemoption to generate an SSH key in the RSA format. Otherwise,ssh-keygen使用默认的OpenSSH格式创建SSH键,该格式在R2021A之前不支持MATLAB版本。金宝app例如,在终端窗口中,输入此命令:

    ssh-keygen -m PEM
    If you generate an SSH key without specifying the-m Pemoption, you can convert your key to the supported RSA format using this command, where〜/.ssh/id_rsa是SSH密钥文件的名称。
    ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

  3. 将钥匙放在HOME/.sshfolder. To verify whichHOMEfolder MATLAB is working with, in the MATLAB Command Window, enter:

    getenv('HOME'

    Before R2021a, if you created the SSH key using the-m Pem选项,您可以跳过步骤4。

  4. To make sure that MATLAB detects the SSH keys, in the MATLAB Command Window, enter:

    git = settings().matlab.sourcecontrol.git; git.PrivateKeyFile.PersonalValue ='/path/to/key';git.PublicKeyFile.PersonalValue ='/path/to/key.pub';

    如果你进入了一个密码,当生成党卫军H key, configure MATLAB to use the passphrase and receive a prompt only once per session. To do so, use an SSH agent or, in the MATLAB Command Window, enter:

    git.KeyHasPassphrase.PersonalValue = true;

    To use multiple keys, use an SSH agent. If an SSH agent is running, MATLAB looks for keys in the agent before looking inHOME/.ssh

  5. Configure your GitHub or GitLab account to use the SSH keys. To do so, go to the。ssh文件夹并复制内容.pubfile. Then, go to your account settings, and in theSSH键section, paste the contents of the.pub归档到添加SSH键场地。

安装Git凭证助手

要配置MATLAB以记住使用GIT时的用户名和密码,请安装凭证助手。所有平台的推荐凭证助理是GIT凭据管理器Core。

例如,to install Git Credential Manager Core on a Windows system, download and run the Git for Windows installer using the instructions described in安装Command-Line Git Client。在里面Choose a credential helper安装程序的部分,选择Git Credential Manager Coreas the credential helper.

禁用压缩金宝appModels

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.

要与新的SLX文件一起使用此设置,请使用具有SLX压缩设置为无的模型模板创建模型。对于现有的SLX文件,设置压缩,然后保存模型。有关更多信息,请参阅设置SLX压缩水平

ConfigureGit使用GitLFS

If you are working with large files, configure Git to use Git Large File Storage (LFS) by installing command-line Git and setting up LFS.

例如,要使用Windows系统上使用Git LFS,请使用中描述的说明下载Windows安装程序的Git安装Command-Line Git Client。在里面选择组件section of the Git for Windows installer, select thegit LFS(大文件支持)金宝appandAssociate .sh files to be run with Bash选项。

MATLAB不支持Git LFS金宝app锁定。此外,MATLAB不支持LFS命令,例如金宝appgit lfs track。Use!gitlfs track反而。

笔记

Git LFS uses Git hooks. Make sure that you have Cygwin installed. For more information, seeConfigure MATLAB for Git on Windows

ConfigureGit使用MATLABfor Diff and Merge

You can configure Git to use the MATLAB Comparison Tool for diff and merge. The MATLAB Comparison Tool provides tools for merging MathWorks®files such as live scripts, MAT, SLX, or MDL files. You can use the automerge tool with Git to automatically merge branches that contain changes in different subsystems in the same SLX file.

For more information about how to configure Git to use the MATLAB Comparison Tool, seeCustomize External Source Control to Use MATLAB for Diff and Merge

安装Command-Line Git Client

To use Git LFS or a credential helper, you must install a command-line Git client and make it available system-wide. Before R2020b, a command-line Git client must be installed to use Git to merge branches in MATLAB.

要检查是否安装了git,请输入命令!git在MATLAB命令窗口中。如果命令不返回任何内容,则需要安装命令行Git。This table provides instructions on how to install command-line Git based on your operating system.

Operating System Instructions
Windows
  1. Download and run the installer fromhttps://gitforwindows.org/

  2. 在里面Adjusting your PATH environmentsection, choose the install optionGit from the command line and also from 3rd-party software。此选项将git添加到您的小路variable and makes it available system-wide so that MATLAB can communicate with Git.

  3. 在里面section for configuring the line-ending conversions, choose the结帐窗口风格,提交Unix风格的线路结尾选项。线路结束格式在计算机和用户之间不可强制执行,但是您可以支持在此处的文本文件的一致线结尾金宝app。gitattributes每个存储库的文件。

  4. Restart your system for the changes to take effect.

Linux

Git is available for most distributions. Install Git for your distribution. For example, on Debian®,,,,install Git by entering:

sudo apt-get安装git
macOS On Mavericks (10.9) or above, run Git from a Terminal window. If you do not have Git installed already, it will prompt you to install Xcode Command Line Tools. For more information, seehttps://git-scm.com/doc

Related Topics