Retrieving oldest commit with JGit

I am experimenting with JGit for a project and while it mostly works, retrieving the oldest (first) commit does not. Here is the code:

    RevWalk rw = new RevWalk(new Repository(
           new File("/path/to/git")));
    RevCommit oldest;
    Iterator<RevCommit> i = rw.iterator();
    if (i.hasNext())
        oldest =;
    Commit c = oldest.asCommit(rw); //oldest is null here, NPE

Does anyone know what I am doing wrong?

  • git: export from bitbucket and import github (w/ commits)
  • git commit and push via batch file on Windows
  • Using Git when I want my branches to be always updated
  • Git rewind master branch to specific commit in master's history
  • What does this mean from a security perspective: “Caution: This repository is private but the published site will be public.”
  • Git - committing changes in master to a new branch
  • can't commit with sublime text git on Ubuntu 14.04
  • Can git merge upstream changes both to a file and a copy within a repository?
  • “git push all” (multiple repositories) doesn't work
  • Proper way to release in Git with submodules
  • Git is ignoring .idea folder, but that isn't in gitignore
  • Github wiki and tags
  • One Solution collect form web for “Retrieving oldest commit with JGit”

    I think I found it. You have to reverse the commit log and set a starting point in order to make it start going through the revisions. The following extract does what I was looking for, but I somehow doubt how optimal it is.

     RevWalk rw = new RevWalk(new Repository(
           new File("/path/to/git")));
     RevCommit c = null;
     AnyObjectId headId;
     try {
         headId = git.resolve(Constants.HEAD);
         RevCommit root = rw.parseCommit(headId);
         c =;
     } catch (IOException e) {
    Git Baby is a git and github fan, let's start git clone.