Manu Raghavan

The light of morning decomposes everything.

Make MATLAB & Git Play Well Together

OK, so I don’t plan to wax eloquent over the virtues of distributed source control. I’ll leave the platitudes to Linus Torvalds.

If you have been wary of setting up a source control system in the MATLAB environment because trying to leaves you stupefied and a few hours older with little to show for it, you are not alone. As documented here, here and here.

I’m sure there are plenty of reasons as to why source control has been onerous for amateur programmers, but Git makes a number of them go away. Branching/merging/rebase maneuvers don’t require you or your collaborators to be connected at all times, freeing you up to work in airplanes and trains without internet connectivity.

You can read a blow by blow comparison of Git with its competitors on “Why Git is better than X”, if you need more convincing.

If you’re beginning to get serious about source control for your MATLAB projects, here is a function that might be useful to you: git.m

A thin MATLAB wrapper for the Git source control system

Short instructions

  1. Get the open-source Git application for your OS.

  2. Download git.m to your MATLAB path and use git at the MATLAB prompt exactly as you would use the OS command prompt.

Long instructions

Here’s a common MATLAB workflow to create your Git repository, add a few files, modify them, and commit your changes:

(git_workflow.m) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
% Creates initial repository tracking all files under some root folder
cd ~/
git init

% Shows changes made to all files in repo (none so far)
git status

% Create a new file and add some code
edit foo.m

% Check repo status, after new file created
git status

% Add foo.m to your repo
git add foo.m

% Commit your changes to a new branch, with comments
git commit -m 'Created new file, foo.m'


% Other useful commands (replace ellipses with appropriate args)
git checkout ...       % To return to an earlier node in branch
git branch ...         % To create or move to another branch
git merge ...          % When merge conflicts arise
git diff ...           % See line-by-line changes

This is not meant to be a comprehensive guide to the extensive git toolchain. Take a look at the original Git documentation or one of the other related resources below.

  1. GitX: A visual interface for Git on the OS X client
  2. Github.com: Remote hosting for Git repos
  3. Everyday GIT With 20 Commands Or So

Comments