MATLAB通过命令窗口使用git

10
我在编程开发中使用MATLAB的Git支持,经常进行提交、推送和所有标准源代码控制操作。
然而,我只使用过MATLAB的用户界面,基本上是通过右键单击文件夹并浏览菜单,直到找到正确的选项为止(见下图)。
有没有办法让MATLAB的命令窗口运行git命令,而不需要每次都浏览菜单?

enter image description here

3个回答

11
您可以在MATLAB中使用系统命令行转义符号!来执行git命令。例如:
!git status
!git commit -am "Commit some stuff from MATLAB CLI"
!git push

你需要在系统上安装Git才能使用此功能。

5
可以,虽然不是专门使用Git:https://uk.mathworks.com/help/matlab/matlab_external/run-external-commands-scripts-and-programs.html另一个有用的命令是system。它允许返回状态和结果:[status, result] = system('git status'):https://uk.mathworks.com/help/matlab/ref/system.html - tuna_fish
我偶然发现这个问题的解决方法也可以解决我的另一个问题:https://stackoverflow.com/questions/42271641/matlab-git-pull。请随意在那里回答。 - Ander Biguri
哦,是的,“system”。我喜欢避免使用它,因为它可以运行任何东西(rm -rf)。直接调用“git”似乎是一个更好的主意。 - Ander Biguri
有没有办法让 ML 在没有使用 -m 提交时不会冻结,从而打开 vi? - KitsuneYMG
有没有一种方法可以将文件推送到外部的GitHub存储库?我遇到了这个问题 https://stackoverflow.com/questions/75370277/commiting-and-pushing-files-from-matlab-to-github-using-matlab-script - forgottofly

8
我喜欢将以下函数添加到我的路径中:
function varargout = git(varargin)
% GIT Execute a git command.
%
% GIT <ARGS>, when executed in command style, executes the git command and
% displays the git outputs at the MATLAB console.
%
% STATUS = GIT(ARG1, ARG2,...), when executed in functional style, executes
% the git command and returns the output status STATUS.
%
% [STATUS, CMDOUT] = GIT(ARG1, ARG2,...), when executed in functional
% style, executes the git command and returns the output status STATUS and
% the git output CMDOUT.

% Check output arguments.
nargoutchk(0,2)

% Specify the location of the git executable.
gitexepath = 'C:\path\to\GIT-2.7.0\bin\git.exe';

% Construct the git command.
cmdstr = strjoin([gitexepath, varargin]);

% Execute the git command.
[status, cmdout] = system(cmdstr);

switch nargout
    case 0
        disp(cmdout)
    case 1
        varargout{1} = status;
    case 2
        varargout{1} = status;
        varargout{2} = cmdout;
end

你可以直接在命令行中输入 git 命令,不需要使用 !system。但它还有另一个优点,就是你可以静默调用 git 命令(不输出到命令行),并带有状态输出。如果你正在创建用于自动化构建或发布流程的脚本,这将非常方便。


1
你可以使用“命令风格”调用它 - 即在MATLAB命令行中键入git commit -m“mycomment”,或者你可以使用“函数风格”调用它 - 即使用[status, cmdout] = git('commit', '-m', '"mycomment"');。如果我只是交互式地工作,我会使用第一种风格,但如果我正在构建自动化构建或发布脚本,我会使用第二种风格。 - Sam Roberts
作为一个例子,我最近与客户合作创建了一个构建和发布脚本。该脚本调用 git status 确保本地的 dev 与远程的 dev 同步;调用 git checkout -b 创建一个发布分支;更新 Contents.m 文件中代码的版本号;检出 devmaster 分支,并调用 git merge 从发布分支合并代码;调用 git push 推送 devmaster 和发布分支到远程;最后检出 master 并调用 makefile 使用 MATLAB Compiler 构建一个工件。 - Sam Roberts
所有这些都是使用“函数式风格”完成的,使用status输出来检查每个步骤是否成功。函数式风格确保没有输出到命令行,因此构建脚本可以静默运行。 - Sam Roberts
哇,我需要为我的软件(TIGRE)实现这个功能,以便有一个适当的构建和发布方法。 - Ander Biguri
1
仅为完整起见:Mathworks文件交换中至少有另外两个git包装器可用,即此处此处。然而,我更喜欢@SamRoberts的解决方案,因为它在可选输出参数方面非常灵活。为什么不将其发布到文件交换中呢? - ToJo
显示剩余2条评论

1
从23b开始,有一个不需要Git安装的MATLAB Git API:gitrepo/gitclone
使用示例:
>> repo = gitrepo(pwd);
>> add(repo,"newScript.m");
>> commit(repo,Files="newScript.m",Message="Commit new file");

克隆一个代码库:
>> repo = gitclone("https://github.com/myuser/myrepo");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接