JGit: Can't find a tutorial or simple example

I can not find a working tutorial for JGit.

It would be very helpful if someone had a good link or a simple example (f.ex. cat-a-file) that works (answer from How to "cat", the file in JGit? Does not compile with JGit V. 1.0).

Unfortunately, the official JGit documentation for beginners is not very useful to me. But I think there should be a lot of developers who use JGit?

+75
documentation jgit
Jul 28 '11 at 15:51
source share
5 answers

In fact, there is very little API documented use.

For other readers, I hereby give a simple test for the most common operations:

  • Create repository
  • Clone it
  • to add a file
  • Fix
  • Click
  • Track origin/master in master (this is necessary if you are cloning a bare repo)
  • Pull (useless, in this case, but whatever)

In particular, note that adding a file requires a template , not a path. Additionally, tracking requires .setForce(true) due to the existence of master on the clone.

Please note that this example should be simple and self-sufficient.

 import java.io.File; import java.io.IOException; import org.eclipse.jgit.api.*; import org.eclipse.jgit.api.errors.*; import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode; import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.Repository; import org.junit.Before; import org.junit.Test; public class TestJGit { private String localPath, remotePath; private Repository localRepo; private Git git; @Before public void init() throws IOException { localPath = "/home/me/repos/mytest"; remotePath = "git@github.com:me/mytestrepo.git"; localRepo = new FileRepository(localPath + "/.git"); git = new Git(localRepo); } @Test public void testCreate() throws IOException { Repository newRepo = new FileRepository(localPath + ".git"); newRepo.create(); } @Test public void testClone() throws IOException, GitAPIException { Git.cloneRepository().setURI(remotePath) .setDirectory(new File(localPath)).call(); } @Test public void testAdd() throws IOException, GitAPIException { File myfile = new File(localPath + "/myfile"); myfile.createNewFile(); git.add().addFilepattern("myfile").call(); } @Test public void testCommit() throws IOException, GitAPIException, JGitInternalException { git.commit().setMessage("Added myfile").call(); } @Test public void testPush() throws IOException, JGitInternalException, GitAPIException { git.push().call(); } @Test public void testTrackMaster() throws IOException, JGitInternalException, GitAPIException { git.branchCreate().setName("master") .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM) .setStartPoint("origin/master").setForce(true).call(); } @Test public void testPull() throws IOException, GitAPIException { git.pull().call(); } } 
+138
Apr 25 '12 at 7:19
source share

I created a jgit-cookbook , i.e. A set of simple stand-alone recipes that you can use to perform various actions in JGit. It should already contain the basic things you do with JGit, welcome suggestions for new snippets!

+46
Aug 29 '13 at 6:36 on
source share

Here is a very good Getting Started tutorial: http://alblue.bandlem.com/2013/11/embedding-jgit.html

+3
Nov 22 '13 at 9:21
source share

I wrote an article Getting Started with JGit , in which through the JGit API you can create or clone a repository, extract changes, add and remove files from your history, view the history, and finally return to the original repository.

If you prefer to read only the source code, the article uses educational tests to demonstrate the use of the API. The source code has no dependencies outside of JGit, so it can be inserted as an optional IDE. A complete list can be found here: https://gist.github.com/rherrmann/433adb44b3d15ed0f0c7

While the article does not cover all the details, it refers to more detailed information that I published earlier when it is available. For example, when it comes to authentication , access , initialization , cloning repositories or creating diffs .

+2
Dec 15 '15 at 10:47
source share
0
Oct 23 '13 at 23:45
source share



All Articles