The JGit framework is rich and diverse, it has two layers, a low-level api and a higher-level set of porcelain commands. This can be a bit intimidating at first as there are lots of classes, some of which are not relevant for most tasks.
package com.javavillage.gitrepo; import java.io.File; import java.io.IOException; import org.dstadler.jgit.helper.CookbookHelper; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; /** * Simple snippet which shows how to open an existing repository * * @author Soha */ public class GitRepository { public static void main(String[] args) throws IOException, GitAPIException { // first create a test-repository, the return is including the .get // directory here! File repoDir = createGitRepo(); // now open the resulting repository with a FileRepositoryBuilder FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(repoDir).readEnvironment() // scan // environment // GIT_* // variables .findGitDir() // scan up the file system tree .build(); System.out.println("Having repository: " + repository.getDirectory()); // the Ref holds an ObjectId for any type of object (tree, commit, blob, // tree) Ref head = repository.getRef("F:/repo"); System.out.println("Ref of F:/repo: " + head); } private static File createGitRepo() throws IOException, GitAPIException { Repository repository = GitHelper.openRepository(); System.out.println("Temporary repository at " + repository.getDirectory()); // create the file File myfile = new File(repository.getDirectory().getParent(), "testfile"); myfile.createNewFile(); // run the add-call Git git = new Git(repository); git.add().addFilepattern("testfile").call(); // and then commit the changes git.commit().setMessage("Added testfile").call(); System.out.println("Added file to repository at " + repository.getDirectory()); File dir = repository.getDirectory(); return dir; } }
Helper class which can use across git project
package com.javavillage.gitrepo.helper; import java.io.File; import java.io.IOException; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; public class GitHelper { public static Repository openRepository() throws IOException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(new File("F:/repo")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); return repository; } public static Repository createNewRepository() throws IOException { // prepare a new folder File localPath = File.createTempFile("F:/repo", ""); localPath.delete(); // create the directory Repository repository = FileRepositoryBuilder.create(new File("F:/repo", ".git")); repository.create(); return repository; } }
If any file is created, if u want to add it index use below snippet for that
Repository repository = GitHelper.createNewRepository(); Git git = new Git(repository); // create the file File myfile = new File(repository.getDirectory().getParent(), "testfile"); myfile.createNewFile(); // run the add-call git.add() .addFilepattern("testfile") .call(); System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
Now will try running above code to see jgit basic example, try to install eclipse plugin as per below link
Git eclipse pluginWithout having any basic folder in git repository if we run above code will get below error.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Temporary repository at F:\repo Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:997) at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:267) at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1053) at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:142) at com.javavillage.gitrepo.GitRepository.createGitRepo(GitRepository.java:53) at com.javavillage.gitrepo.GitRepository.main(GitRepository.java:25)
Follow below steps to commit ur code from eclipse. Use share project option
Select repository for to commit the code
After that right click on project goto Team -> commit the code
Once execution done you can see file which got committed to git.
GitHub is a Web-based Git repository hosting service. It offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features.
Learn MoreJGit is the Java implementation of Git. It is a library, that also can be used in your own applications. It also provides some sort of CLI operations. EGit on the other side is the Eclipse team provider plugin for Git, which uses JGit as Git implementation
Learn MoreEGit is an Eclipse Team provider for the Git version control system. Git is a distributed SCM, which means every developer has a full copy of all history of every revision of the code, making queries against the history very fast and versatile.
Learn More