Home > Software Development > How to Read GIT Repository Using Java APIs

How to Read GIT Repository Using Java APIs

January 27th, 2013 Leave a comment Go to comments

GIT is a source code control system created by Linus and others for managing Linux kernel development. It becomes one of the most popular version control systems especially in the open source community. Most developers use command line or the plugins to IDEs like Eclipse, NetBeans. I think even Microsoft VisualStudio has add-on for connecting to GIT, but I haven’t checked it.

If you want more control or integrate GIT with your application, you can use Java APIs (or APIs in other programming languages) connecting to GIT repositories as well. One of the Java implementations is the JGit APIs which is now a project under the Eclipse foundation. The structure and format of the GIT repositories are well defined. You can use any programming languages to access the repository.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


As I said many times, APIs are views to the products which are models (think MVC). If you are not familiar with GIT yet, you may want to read this introduction by Charles Duan. I found it’s very well written and probably the best introduction on GIT you can find.

Once that is clear, let’s move on to a sample which read the content of a file in the HEAD revision. The JGIT API I used is org.eclipse.jgit- I assume this sample should be easy to read through. If not, feel free to give it a try and leave a comment.

/** GIT sample code with BSD license. Copyright by Steve Jin */

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;

public class JGitPrintContent
  public static void main(String[] args) throws Exception
    File gitWorkDir = new File("C:/temp/gittest/");
    Git git = Git.open(gitWorkDir);
    Repository repo = git.getRepository();

    ObjectId lastCommitId = repo.resolve(Constants.HEAD);

    RevWalk revWalk = new RevWalk(repo);
    RevCommit commit = revWalk.parseCommit(lastCommitId);

    RevTree tree = commit.getTree();

    TreeWalk treeWalk = new TreeWalk(repo);
    if (!treeWalk.next()) 
      System.out.println("Nothing found!");

    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repo.open(objectId);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    System.out.println("file1.txt:\n" + out.toString());

  1. January 27th, 2013 at 23:48 | #1

    How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/1ycfbVxU

  2. January 27th, 2013 at 23:55 | #2

    How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/3VEYyXwl

  3. January 28th, 2013 at 12:05 | #3

    How to Read #GIT #Repository Using #Java #APIs http://t.co/kGsVMbup via @sjin2008

  4. January 28th, 2013 at 13:27 | #4

    How to Read GIT Repository Using Java APIs http://t.co/Ac4giWev

  5. January 31st, 2013 at 11:08 | #5

    How to Read GIT Repository Using Java APIs http://t.co/as2Dx30H #IT CIO

  6. samuel
    May 23rd, 2014 at 09:28 | #6

    how to connect to remote GIT repository by SSH? how to do the authorization?

  7. May 23rd, 2014 at 10:15 | #7


    Very good question. I haven’t tried that. Should you find out, please feel free to post the answer here. I am interested to know as well.


  8. Navaraj
    July 10th, 2014 at 15:40 | #8

    Hi Steve,

    Thank you for the nice code. I tried and it works well. Can you please elaborate how commit(done by others) is read from HEAD file? For example: when I pull from a shared repo and run commit.getFullMessage() I get the last commit message from other users too, but when I check HEAD file I cannot see the commit message from other users instead I just see the pulled message.
    Also I did not find any documentation for the recommended API, is there any better documented API available now?

    Thanks in advance.

  9. July 13th, 2014 at 00:00 | #9

    Hi Navaraj,

    Glad it worked for you. I haven’t touched the API for a while and kind forget what happened there. You may want to check out Eclipse or Netbeans plugin as they use the API extensively.


  10. Haridwar Jha
    March 30th, 2015 at 08:04 | #10

    Supoose that i have three eclipse project in my repository, through them i have made some changed in one project then while committing , it’s scanning all repository. i want to scan only selected project in repository. how can i do this…????

  11. Hiba
    November 30th, 2015 at 02:00 | #11

    I got the following exception:
    Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.eclipse.jgit.util.FS.(FS.java:113)
    at org.eclipse.jgit.api.Git.open(Git.java:99)
    at test2.JGitPrintContent.main(JGitPrintContent.java:32)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 3 more

  12. December 4th, 2015 at 12:33 | #12

    It seems you want to include the slf4j package into your classpath. -Steve

  13. ramya
    April 7th, 2017 at 00:40 | #13

    Is it possible to read git repo files without cloning?

  14. aditya
    June 20th, 2017 at 03:32 | #14

    how to move svn repo to git repo using java code how?

  1. January 28th, 2013 at 02:51 | #1
  2. February 3rd, 2013 at 23:18 | #2