Home > Software Development > GIT Commands Using Java APIs

GIT Commands Using Java APIs

March 26th, 2013 Leave a comment Go to comments

In previous posts, I’ve covered an Java API called JGIT for working with GIT. Besides the low level APIs, JGIT also supports high level command line like interfaces. If you know how to use the GIT command line, you can use the APIs. Note that it’s different from calling GIT commands or any other commands from Java code.

Here is a sample code I wrote to try out the APIs. Give it a try and you will like these high level APIs.

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

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.ReflogCommand;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.api.errors.UnmergedPathsException;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.ReflogEntry;

public class JGitSample
  public static void main(String[] args) throws Exception
    File gitWorkDir = new File("C:/temp/gittest/");
    Git git = null;

    InitCommand initCommand = Git.init();
    git = initCommand.call();

    git = Git.open(gitWorkDir); // not necessary, but show how to open an existing repo
    changeContentAndCommit(git, "DoubleCloud.org rocks!", "first commit");
    changeContentAndCommit(git, "DoubleCloud.org really rocks!", "second commit");
    changeContentAndCommit(git, "DoubleCloud.org really really rocks!", "third commit");
    Iterator<RevCommit> iterator = git.log().call().iterator();

    RevCommit rc2 = iterator.next();
    System.out.println("msg2:" + rc2.getFullMessage());
    System.out.println("con2:" + new String(rc2.getRawBuffer()));
    System.out.println("msg1:" + iterator.next().getFullMessage());
    ReflogCommand reflogCmd = git.reflog();
    Collection<ReflogEntry> reflogs = reflogCmd.call();
    System.out.println("size of reflogs:" + reflogs.size());

  private static void changeContentAndCommit(Git git, String content, String message) throws NoHeadException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, GitAPIException 
    File workDir = git.getRepository().getWorkTree();
    File myfile = new File(workDir, "file1.txt");
    writeToFile(myfile, content);

    AddCommand add = git.add();

    CommitCommand commit = git.commit();

  public static void writeToFile(File file, String text)
    FileWriter writer;
	    writer = new FileWriter(file);
    catch (IOException e)

Categories: Software Development Tags: ,
  1. March 26th, 2013 at 01:30 | #1

    GIT Commands Using Java APIs (DoubleCloud) http://t.co/5hEqD4liTq

  2. shasu
    July 18th, 2013 at 07:35 | #2

    While i am reading from a Repository using JGIT me getting following error.

    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -20
    at org.eclipse.jgit.util.NB.decodeInt32(NB.java:96)
    at org.eclipse.jgit.lib.ObjectId.fromRaw(ObjectId.java:175)
    at org.eclipse.jgit.treewalk.AbstractTreeIterator.getEntryObjectId(AbstractTreeIterator.java:400)
    at org.eclipse.jgit.treewalk.TreeWalk.getObjectId(TreeWalk.java:654)
    at JGitPrintContent2.main(JGitPrintContent2.java:72)

    could you help

  3. shasu
    July 18th, 2013 at 07:37 | #3

    Where can i get some good number of documentations or samples of JGIT .
    Can you please suggest me ?

    Thank you.

  4. July 18th, 2013 at 13:38 | #4

    I guess the index used to get element of an array is out of range. You may want to check the value passed in.


  5. July 18th, 2013 at 13:40 | #5

    It’s pretty hard to find samples and docs. I remember there are some unit test code that can serve as samples. You can search them over the Internet.

  6. Jgit API User
    July 29th, 2013 at 04:19 | #6

    Hi ,
    the example above commits a file , recommits it and we get the UID . the content can be accessed withthe corresponding UID . but i have to commit more than 1 file at a time . the respective UId are also obtaine d. but while reading the repository to retrieve the files , even on giving different UId’s i am getting the same file . to be correct the first file only. please help .

  7. sashu
    July 29th, 2013 at 07:24 | #7

    how to commit more than 1 file in the repository. and retrieve the files by their individual commit ids.

  8. michael
    August 5th, 2013 at 01:58 | #8

    hi Steve Jin,
    waiting for your help . please let us know how to commit more than 1 file at a time in the repository.and retrieve them individually by respective commit id’s

  9. August 12th, 2013 at 02:05 | #9

    Hi Michael,

    I think committing more than one file has its corresponding command and thus Java APIs. To retrieve individually you may want to do more research. I haven’t touched the API for a while.


  1. March 26th, 2013 at 11:19 | #1