Ander Biguri
Ander Biguri

Reputation: 35525

MATLAB git by command window

I use MATLABs git support in the development of my code, often committing, pushing and all the standard source control stuff.

However, I have only used MATLABs user interface that basically works by right clicking on the folder and navigating through the menu until the right choice is found (see image below).

Is there a way to make MATLABs command window run git commands, instead of needing to navigate trough the menu each time?

enter image description here

Upvotes: 12

Views: 11467

Answers (3)

lrm29
lrm29

Reputation: 498

From 23b there is a MATLAB Git API that does not require a Git installation: gitrepo/gitclone.

Example usage:

>> repo = gitrepo(pwd);
>> add(repo,"newScript.m");
>> commit(repo,Files="newScript.m",Message="Commit new file");

To clone a repository:

>> repo = gitclone("https://github.com/myuser/myrepo");

Upvotes: 2

Sam Roberts
Sam Roberts

Reputation: 24127

I like to put the following function on my path:

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

You can then type git commands directly at the command line, without using ! or system. But it has an additional advantage, in that you can also call the git command silently (no output to the command line) and with a status output. This makes it quite convenient if you're creating a script for an automated build, or release process.

Upvotes: 9

tuna_fish
tuna_fish

Reputation: 437

You can use the system command line escape ! for git commands within MATLAB. Eg:

!git status
!git commit -am "Commit some stuff from MATLAB CLI"
!git push

You'll need to have Git installed on your system for this to work.

Upvotes: 12

Related Questions