- Undocumented Matlab - https://undocumentedmatlab.com -

JGit-Matlab integration

Posted By Yair Altman On July 10, 2013 | 6 Comments

I would like to introduce guest blogger Mark Mikofski [1], of SunPower Corp. Mark has developed the JGit4MATLAB utility, which can be used to integrate Matlab with the popular JGit open-source version-control system.


JGit logo
JGit logo
JGit [2] is an open-source Java implementation of Git [3], a popular distributed version control system. Since Matlab is essentially a Java interpreter, JGit is an obvious candidate for integrating Git into Matlab. Luckily, JGit also comes with a porcelain class that has the most popular commands conveniently packaged. This is the starting point for JGit4MATLAB [4] – a thin Matlab wrapper utility on the Matlab File Exchange.
The first step is to download and install JGit4MATLAB in your Matlab path. The first time that the JGit Matlab class is called, it downloads the latest version of org.eclipse.jgit.jar, adds it to Matlab’s static Java classpath and makes a backup of the existing javaclasspath.txt file as javaclasspath.JGitSaved. Matlab must then be restarted for the changes to the static Java class path to take effect. From now on, you can use the commands in the JGit Matlab class or the jgit.m wrapper function. These use basic JGit commands, which shall be described below:
Most of the important JGit methods are encapsulated in the Matlab JGit wrapper class and jgit.m function, but you can also use them directly if you wish. The Matlab wrapper currently supports ADD, BRANCH, CHECKOUT, CLONE, COMMIT, DIFF, INIT, LOG, MERGE & STATUS, but there are many more in org.eclipse.jgit.api.Git that you can use directly. In fact you don’t really need to use the wrapper at all if you don’t want to – all you really need is the JAR file, which the wrapper downloads and installs for you. You can also download the JAR file independently from here [5] (latest only, which is what JGit4MATLAB does), or here [6] (latest and previous releases). If you’re using JGit4MATLAB, you can update the JAR file to the latest version at will, by issuing JGit.downloadJGitJar('@JGit\org.eclipse.jgit.jar').

The most basic Git commands are clone, init, add and commit. Let us explore how to use JGit’s “Git-Porcelain API [7]” class in Matlab to perform these tasks.

Clone and Init

Clone and Init are both class methods of the Git-Porcelain API class and return a Command API class object, which is what nearly all of the Git-Porcelain methods do:

import org.eclipse.jgit.api.Git   % import the package
cloneCMD = Git.cloneRepository;   % return a CloneCommand object
URI = 'git://github.com/mikofski/JGit4Matlab.git';   % a Git repository
cloneCMD.setURI(URI)   % set the repository to clone
g = cloneCMD.call;   % clone the repository

or more compactly:

g = org.eclipse.jgit.api.Git.cloneRepository.setURI(URI).call;

The call method executes the command with the options and arguments set by the other methods. Calling the clone method also returns a Git instance. The full API for the CloneCommand class [8] lists all the possible options and arguments for cloning. One option lets you set an optional progress monitor; there are some already included in JGit, but unfortunately, they use the carriage return (or ‘\r’) escape sequence which in Windows is the same as a newline (read here [9]). So, I implemented a customized ProgressMonitor class [10] for Matlab that uses backspace (or ‘\b’) instead.
Init is very similar to clone:

g = org.eclipse.jgit.api.Git.init.call;

Configuring author information

Before moving on, it is important to set the “author” and “email” fields in the Git config. JGit will use your computer’s info if you provide nothing. You probably want to set these values globally, so the place to put them is in your $HOME folder in the “.gitconfig” file.

file = java.io.File(fullfile(getenv('home'),'.gitconfig'));
gitconfig = org.eclipse.jgit.storage.file.FileBasedConfig(file, org.eclipse.jgit.util.FS.DETECTED);
gitconfig.setString('user',[], 'name','John Doe');       % place your name in here
gitconfig.setString('user',[], 'email','john@doe.com');  % place your email in here

Hopefully your commits will all have the right name and email address in them from now on.

Typical JGit Workflow

Now that you have a Git repository, you will want to start working with it. The typical workflow is to create a feature branch in which to work. But first JGit needs a Git instance. If you saved it from cloning or initializing a new repo then good for you! But if you need to recreate it, just pass the constructor a File [11] object that points to the .git folder inside your Git repo:

JGit workflow
JGit workflow

file = java.io.File(fullfile('path', 'to', 'repo', '.git'));
g = org.eclipse.jgit.api.Git.open(file);

Now you are no longer on the “master” branch, which is usually reserved for buildable/deployable code. Start working. But before you do too much, commit your work to your branch:

g.commit.setMessage('initial dump').call;

JGit commit log
JGit commit log

All commits should have a message, so you know what work was completed in that commit. Branch and commit frequently. You can always clean them up later. There are lots of options for adding and committing files, so look at the API’s AddCommand [12] and CommitCommand [13] classes.
Everything else is pretty much the same as above: Get a Git instance; create a command; set the options; and finally call the command:

file = java.io.File(fullfile('path', 'to', 'repo', '.git'));
g = org.eclipse.jgit.api.Git.open(file);
r = g.getRepository;  % a Repository instance can be very useful

Note: Repository.resolve() [14] is one of the most powerful methods.
The JGit log window on the left can be gotten using glog – the standalone JGit command line log GUI (slightly similar to gitk) that can be downloaded as a binary for systems with /bin/sh from the eclipse site [15] (same place as the jar download).
Some tricks can be gleaned by what little work I’ve done so far on the JGit4MATLAB wrapper on the Matlab File Exchange. I’ll be happy to answer whatever questions I can there, or comments here. I’m also happy to accept pull requests on Github [16], of course using JGit4MATLAB!


You can only use an SSH paired keys to authenticate remote repositories, the keys must **not** have a pass-phrase, they must be in the openSSH format and they must be in $HOME/.ssh. PuTTYgen [17] let’s you do all of this, so it’s really not an issue. Of course you can still use remotes that do not require authentication with no problems.
All commands that deal with remotes and that require authentication, e.g. CLONE, FETCH, PULL, PUSH, will only work

  • with SSH
  • with *no* passphrase
  • with keys in the openSSH format
  • with keys and known hosts in $HOME\.ssh

Obviously remotes that do not require authentication will work fine. e.g. public, read-only and local repositories.
SSH is very easy to set up:

  1. Download puttygen [17] (Intel x86). Disregard the “Intel x86” business, it doesn’t matter what processor you have or whether your os is 64-bit or 32-bit. Puttygen is a very mature well establish application that is used by many other applications, for example all of the TortoiseXXX scm clients use Putty, Plink and Pageant. Here is the actual download here (click to download then click to install): http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe [18]
  2. Create a folder called .ssh in your %– USERPROFILE%. On Windows 7 this is C:\Users\ and on XP it is C:\Documents and Settings\. You can do this in Matlab: mkdir(fullfile(getenv('userprofile'), '.ssh')).
  3. Create a key using puttygen, this is fairly self explanatory, and kind of fun, but do *not* set a pass-phrase; leave those fields blank!
  4. Using puttygen, find the conversion menu tab and export the key in the openSSH format to the .ssh folder that you created earlier.
  5. Now copy and paste your public key to your remote repositories (Github, Bitbucket, etc.) as needed. Jsch, which is the ssh client that JGit uses, should now work out of the box.

Unfortunately using SSL (HTTPS) isn’t possible with JGit4MATLAB. Sorry. but hopefully you’ll love the excitement of using ssh key pairs!

Further development

Possible future work on the Matlab-JGit integration includes assigning standard keyboard-shortcuts [19] to common source-control tasks, integration as toolbar/toolstrip icons for easier access, and in the Matlab Editor in general. Only a few days ago, a reader on this blog has shown interest [20] in this.
In the far future, integration with Matlab’s existing source-control system [21]. Anyone wish to pick up the glove?
JGit is covered by the BSD 3-clause license [22] which is very permissive, and should allow MathWorks or anyone for that matter to develop an integrated JGit application for Matlab. There are more packages in JGit than just the porcelain API, including Swing utilities and other utilities that can be used to create rich user interfaces and experiences, but the development would have to be in Java. Developers should look at the developer’s section of the JGit website and use either the Maven or Github repository to set up their forks.


JGit and Matlab are a natural combination. JGit is a mature and thorough open-source project. Most porcelain commands are available without any additional work. There are some arcanae, but the API documentation is sufficient to figure out most things using short high-level code. If you find yourself diving too deep, then scan the docs some more, and you will probably find a shortcut.

Categories: Desktop, Guest bloggers, Java, Low risk of breaking in future versions

6 Comments (Open | Close)

6 Comments To "JGit-Matlab integration"

#1 Comment By Malcolm Lidierth On July 11, 2013 @ 05:11

Thanks for this article. I have been using Git for a while now to support the Waterloo code I blogged about over the past couple of weeks. The Java code is developed in NetBeans which has full Git support – it would be good to see that also in MATLAB.
Being far from expert in Git, I find the GitHub app GUIs useful (it is not specific to the GitHub site: I use SourceForge). It shows me the Git setup, supports SSH and it’s easy to exclude specific files/folders from the public repo. As all changes are made in the Git repo, they are honoured also in NetBeans/other IDEs/from the command line and I am guessing with JGit/JGit4MATLAB too (?).
MATLAB makes use of NetBeans RCP components such as the editor. It would be good to see the MATLAB file manager provide the sort of version control support that NetBeans provides. That could encourage fuller open-source collaboration amongst the MATLAB community too: a problem with the MATLAB FEX is that it does not presently support that.

#2 Comment By Mark Mikofski On July 11, 2013 @ 11:05

Thanks for your comment.
In response to … “…and I am guessing with JGit/JGit4MATLAB too (?)”, yes, JGit/JGit4MATLAB works with with all Git repositories, both local and remote (i.e.: [29], [30]), regardless of what Git client they were generated with or maintained by, and it inter-operates with the original (C/Perl/shell) Git and other Git-bindings as well (such as Python’s [31] and Java’s JGit).

That’s cool that NetBeans also has Git integration. I use both [32] and [33] a lot, and they both have full [34] integration, e.g.: [3], [35], [36], etc. Maybe this will put some pressure on TMW to finally introduce SCM integration into their IDE, recognizing it’s importance in best coding practices.

In response to your comment about open-source collaboration and FEX, I made a similar comment on CSSM [37].

Frankly, IMHO Github’s window’s app is not the best Git client, I prefer to use [38] on windows, it has a very intuitive and rich feature set that will be very familiar to [39] users. Another client I think is better than Github’s is [40] by Atlassian, maintainers of BitBucket, another online Git/Hg repository host.

BTW: I think the [41] are awesome! Can’t wait to try them out.

#3 Comment By mark mikofski On September 28, 2013 @ 02:34

Just wanted to post an update – I have made some major improvements to JGit4MATLAB. Now it can use SSH passphrases (or not), and can pull, push and fetch. I put utilities in to change or view global config and to save your passphrase. Still more to come! Also check out new website: [42].

#4 Comment By mark mikofski On October 10, 2013 @ 14:52

The latest update is version 0.6 (Falcon):

It allows (subset of) Git syntax to be used.

>> jgit add file1 file2

>> jgit commit -m 'enter msg in single quotes'

>> jgit pull

>> jgit push origin feature

Bugs fixes.
`javaclasspath.txt` is now in prefdir instead of userpath (thanks Volker).
Merge conflicts also output *.BASE, *.LOCAL, *.REMOTE and *.orig for use in merge tools such as meld and p4m.
Documentation is now at [42].

Please submit bugs to [43].


#5 Pingback By JSON-Matlab integration | Undocumented Matlab On August 27, 2014 @ 00:19

[…] would like to once again welcome guest blogger Mark Mikofski. Mark has written here last year about JGIT-Matlab integration (p.s., in the recently-released R2014a, MathWorks added GIT support to Simulink projects, although […]

#6 Pingback By Inter-Matlab data transfer with memcached | Undocumented Matlab On August 27, 2014 @ 05:27

[…] Once again I welcome guest blogger Mark Mikofski. Mark has written here last year about JGIT-Matlab integration and earlier this year on JSON-Matlab integration. Today, Mark shows how to use the well-known memcached library […]

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/jgit-matlab-integration

URLs in this post:

[1] Mark Mikofski: http://undocumentedmatlab.compoquitopicante.blogspot.com

[2] JGit: http://eclipse.org/jgit/

[3] Git: http://git-scm.com/

[4] JGit4MATLAB: http://www.mathworks.com/matlabcentral/fileexchange/41348-jgit4matlab

[5] here: http://eclipse.org/jgit/download/

[6] here: http://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit

[7] Git-Porcelain API: http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/api/Git.html

[8] full API for the CloneCommand class: http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/api/CloneCommand.html

[9] read here: http://undocumentedmatlab.com/blog/command-window-text-manipulation/

[10] ProgressMonitor class: https://github.com/mikofski/JGit4MATLAB/tree/master/%40JGit/MATLABProgressMonitor/src/org/eclipse/jgit/lib

[11] File: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

[12] AddCommand: http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/api/AddCommand.html

[13] CommitCommand: http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/api/CommitCommand.html

[14] Repository.resolve(): http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String)

[15] eclipse site: http://www.eclipse.org/jgit/

[16] Github: https://github.com/mikofski/JGit4Matlab

[17] PuTTYgen: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

[18] http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe: http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe

[19] keyboard-shortcuts: http://www.mathworks.com/help/matlab/matlab_env/keyboard-shortcuts.html

[20] shown interest: http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/#comment-222055

[21] Matlab’s existing source-control system: http://www.mathworks.com/help/matlab/source-control.html

[22] BSD 3-clause license: https://github.com/eclipse/jgit/blob/master/LICENSE

[23] JSON-Matlab integration : https://undocumentedmatlab.com/articles/json-matlab-integration

[24] Matlab-Java interface using a static control : https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control

[25] Spicing up the Matlab Editor : https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor

[26] Using Groovy in Matlab : https://undocumentedmatlab.com/articles/using-groovy-in-matlab

[27] Inter-Matlab data transfer with memcached : https://undocumentedmatlab.com/articles/inter-matlab-data-transfer-with-memcached

[28] Customizing Matlab's Workspace table : https://undocumentedmatlab.com/articles/customizing-matlabs-workspace-table

[29] : https://github.com/

[30] : https://bitbucket.org/

[31] : http://www.samba.org/~jelmer/dulwich/

[32] : http://www.eclipse.org/downloads/

[33] : https://code.google.com/p/spyderlib/

[34] : http://en.wikipedia.org/wiki/Source_Control_Management

[35] : http://subversion.apache.org/

[36] : http://mercurial.selenic.com/

[37] : https://groups.google.com/d/msg/comp.soft-sys.matlab/P1zmmZLi_XM/yF8tXu5DD3UJ

[38] : https://code.google.com/p/tortoisegit/

[39] : http://tortoisesvn.net/

[40] : http://sourcetreeapp.com/

[41] : https://undocumentedmatlab.com/blog/waterloo-graphics-animation-web-deployment/

[42] : http://mikofski.github.io/JGit4MATLAB/

[43] : https://github.com/mikofski/JGit4MATLAB

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.