Pushing changes to a remote repository with JGit

Last Updated : 23 Jul, 2025

JGit is the lightweight and pure Java library implementing the Git version control system. It can allow the developers to perform Git operations programmatically with Java applications. This article will guide you how to push the changes to a remote repository using JGit in the Maven project.

Prerequisites

  • Basic knowledge of Java and Git.
  • JDK is installed in your local system.
  • The Git account with repository to push changes.

Main Concept: Pushing changes to a remote repository with JGit

JGIt is the pure Java implementation of the Git version control system which allows you to interact with the Git repositories programmatically. It can be particularly useful for Java applications that need to automate Git operations such as cloning, committing, and pushing the changes.

Key Components

  1. Repository Initialization: It can access the local repository where changes have been made.
  2. Staging the Changes: It can be used to add the files to staging the area to prepare them for the commit.
  3. Committingaccess the Changes: Creating the commit in the repository history with the staged changes.
  4. Pushing the changes: Uploading the local repository commits to the remote repository.

Implementation of Pushing changes to a remote repository with JGit

Step 1: Create the New Maven Project

Create the new Maven Project using the IntelliJ idea. Once create the project then the file structure looks like the below image.

jgitfile

Step 2: Add the JGit Dependency

Open the pom.xml file and add the JGit dependency into the proejct and re-run the maven for ultilizing the JGit functionalities.

pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.auxentios</groupId>
    <artifactId>JGitExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>22</maven.compiler.source>
        <maven.compiler.target>22</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>6.10.0.202406032230-r</version>
        </dependency>

    </dependencies>
</project>

Step 3: Create the JGitPushExample Class

Java
package com.auxentios;


import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class JGitPushExample {

    private static final Logger logger = LoggerFactory.getLogger(JGitPushExample.class);
    private static final String REMOTE_URL = "https://github.com/iamsyam123/Task-Management-System-using-microservices";
    private static final String LOCAL_REPO_PATH = "C:/Users/Syam/OneDrive/Desktop/Task-Management-System-using-microservices";
    private static final String USERNAME = "iamsyamsh123";
    private static final String PASSWORD = "Passsword@#123@";

    public static void main(String[] args) {
        try {


            // Open the local repository
            File repoDir = new File(LOCAL_REPO_PATH);
            Git git = Git.open(repoDir);

            // Add files to the index
            git.add().addFilepattern(".").call();

            // Commit changes
            git.commit().setMessage("Commit message").call();

            // Push changes to the remote repository
            git.push()
                    .setCredentialsProvider(new UsernamePasswordCredentialsProvider(USERNAME, PASSWORD))
                    .call();

            System.out.println("Changes pushed to remote repository successfully!");

        } catch (IOException e) {
            logger.error("IOException occurred: ", e);
        } catch (GitAPIException e) {
            logger.error("GitAPIException occurred: ", e);
        }
    }
}

Step 4: Run the application

Once complete the project then run the application. It will show the below output as result.

gitlog

This example project can demonstrates to pushing the changes to the remote repository with JGit of the Java maven project.

Conclusion

By following this article, we have learned how to push the changes to the remote repository using JGit in the Maven project. This process can be integrated into the Java applications to manage the Git operations programmatically.

Comment