Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » [help ] new to JGit
[help ] new to JGit [message #659560] Mon, 14 March 2011 15:17 Go to next message
YiFei  is currently offline YiFei Friend
Messages: 6
Registered: March 2011
Junior Member
This term I have to integry JGit to our project for my lab.
when I copy the code from the forum:

RepositoryBuilder builder = new RepositoryBuilder();
File repos = new File("E:\\pmsSpace\\testGit\\test\\basedir\\.git");
Repository repository = builder.setGitDir(repos).build();
Git git = new Git(repository);

LogCommand log = git.log();

for (RevCommit commit : {

then i run it in Eclipse3.5, the error comes out:
org.eclipse.jgit.api.errors.NoHeadException: No HEAD exists and no explicit starting revision was specified

Hope someone could help me out, thx a lot!

Re: [help ] new to JGit [message #659601 is a reply to message #659560] Mon, 14 March 2011 18:51 Go to previous messageGo to next message
Chris Aniszczyk is currently offline Chris AniszczykFriend
Messages: 674
Registered: July 2009
Senior Member
Looks like your repository is empty.

How about doing some commits with CommitCommand?

You can add a file with AddCommand too.
Re: [help ] new to JGit [message #659650 is a reply to message #659601] Tue, 15 March 2011 01:06 Go to previous messageGo to next message
YiFei  is currently offline YiFei Friend
Messages: 6
Registered: March 2011
Junior Member
thx, I do some adds and commits in one function,the code:

File basedir = new File("test/basedir");

RepositoryBuilder builder = new RepositoryBuilder();
builder.setGitDir(new File(basedir, ".git"));

Repository repo =;

Git git = new Git(repo);

new File(basedir, "aa").createNewFile();


.setMessage("besouro automatic message")
.setCommitter("somename", "someemail")

the error still occurs, but if I copy the code out of the function and put it just before theLogCommand code, it runs smoothly, I just cann't figure it out.
Re: [help ] new to JGit [message #659947 is a reply to message #659650] Wed, 16 March 2011 10:13 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 18
Registered: July 2009
Junior Member
That's intentional. Native git behaves the same. Just try out:

cd <tmpFolder>
git init test
cd test
git log

and you will also see an error message about a missing HEAD.

Problem is that newly created repositories where no commit has been done don't have a branch (even master is not yet born) and therefore HEAD is also not pointing to something exisiting. My suggestion is not to call log() on repo's which you know are empty (what would you expect from this call?) or to catch the NoHeadException.

Side topic: you can further streamline your code by using our API also for the repo initialization. See here:

File f = new File(...);
Git git = Git.init().setDirectory(f).call();
try {
	for (RevCommit commit : git.log().call()) {
} catch (NoHeadException e) {
	System.out.println("Empty repo, nothing to log");

Previous Topic:spearce/jgit and jgit
Next Topic:Difference between EGit and JGit
Goto Forum:

Current Time: Sat Apr 04 13:26:26 GMT 2020

Powered by FUDForum. Page generated in 0.02040 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top