Store git command result into a variable using MATLAB script

Issue

I’m trying to get the result after executing the git command function !git config --global user.name which outputs my username in the command prompt of my MATLAB.

Now if I assign the same to a variable to use it in another instance I’m getting the below error as shown below.

userName = !git config --global user.name

Error : Parse error at !git config --global user.name : Usage might be invalid MATLAB syntax

disp(userName);

Solution

You will need something like

[~, out.git_hash] = system('git rev-parse --verify HEAD');
[~, out.git_status] = system('git --no-pager diff --no-color');

etc.

On Windows, depending on different options during setup, you may need to supply the full path to the git binary, e.g.

[~, out.git_hash] = system('"C:/Program Files/Git/mingw64/bin/git"  rev-parse --verify HEAD'); 
[~, out.git_status] = system('"C:/Program Files/Git/mingw64/bin/git" --no-pager diff --no-color'); 

Just be cautious with pager-related parameters.

Answered By – X Zhang

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published