Git commands from Java using JGIT | Programmatically git clone, checkout -b, commit -a, log, status, branch

In this article we will go through very simple basic examples of JGIT library which is fully featured library to use GIT from within Java code programmatically.

Example in this article :

We will do all below steps programmatically through Java main code using JGIT.

  • Clone remote repository from https://github.com into local directory.
  • List all remote branches .
  • Checkout remote ‘develop’ branch into local directory repository.
  • Modify one file in local & verify git status.
  • Commit changes & verify commit log.

Dependency – To execute example in this article you will need maven dependency which you can refer from here.

Remote repository used in example – This code uses this test repository for example purposes. Ravikharatmal/test

JGIT Examples code



Console Output



Verification

As you can see here, we can see repository has been cloned. There are no uncommitted files which means JGIT commit is successful. (Green checkmark icons are added by Tortoise Git).

Here is a quick verification from command line in this directory and you can see the modified file content from code. You can also see that recent commit has been made from JGIT code.




Further Reading

GitHub REST API | Tree API to get remote repo files list & metadata recursively programmatically without cloning in local

GitHub REST API | Get remote repo files list & download file content programmatically without cloning in local

GitHub REST API | Search files, content, pull requests, commits programmatically using Java without cloning

Leave a Reply

Your email address will not be published. Required fields are marked *