Open In App

Deleting branches with JGit

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JGit is lightweight and it is a pure Java library implementing the Git version control system. It can be designed to be embedded in any Java application to provide the Git functionalities. This article will focus on using JGit to delete branches, the common task for developers who need to manage the lifecycle of the feature, hotfix and release the branches in their projects.

Prerequisites

  • Basic knowledge of Java and Git.
  • Java development kit installed in your local system.
  • Maven for building dependency management.

Deleting branches with JGit

Deleting branches in the Git repository is a common task for developers, particularly in managing the lifecycle of the features, fixes and releases. JGit is the pure Java implementation of Git and it can provide the ability to perform this operation programmatically, which can be particularly useful in automated environments or applications where direct Git command-line interactions are not ideal.

JGit's Approach to Branch Deletion

JGit can handle the branch deletion using its API which mirrors the Git command line functionality but within the Java context.

JGit Working

  1. Repository Access: Start by accessing the repository through the Repository instance and necessary for all the subsequent Git operations.
  2. Branch the Delete Command: Use the JGit branchDelete method from the Git class to specify and delete branches. Key parameters include:
    • Branch Names: It can specify the exact names of the branches you want to delete.
    • Force Deletion: Optionally, force the deletion of the unmerged branches by setting the force parameter to true.
  3. Execution: Execution of the deletion with the call method which returns the list of the deleted branch references or throws the exception if the operation fails such as when trying to delete the non-existent or unmerged branch without forcing.

Implementation of Deleting branches with JGit

Step 1: Create the Maven Project

deletefile


Step 2: Add the JGit Dependency

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

    <groupId>org.example</groupId>
    <artifactId>JGit-DeleteBranch</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</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.9.0.202403050737-r</version>
        </dependency>
    </dependencies>

</project>


Step 3: Create the DeleteBranchExample class

Java
package org.example;

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;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class DeleteBranchExample {
    public static void main(String[] args) {
        FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
        try (Repository repository = repositoryBuilder.setGitDir(new File("C:/Users/Mahesh/Desktop/local-repo/.git"))
                .readEnvironment()
                .findGitDir()
                .build()) {
            Git git = new Git(repository);
            String branchName = "feature";
            List<String> call = git.branchDelete()
                    .setBranchNames(branchName)
                    .setForce(true)  // Use force if the branch is not fully merged
                    .call();
            System.out.println("Deleted branch: " + branchName);
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
    }
}


Step 4: Run the application

deletelog-compressed

Conclusion

JGit can offers the practical solution for the programmatically managing the Git operations in the Java applications, including the branch deletion. This functionality can allows the developers to automate the branch management tasks, streamline workflows and maintain the cleaner repositories. Making JGit is the valuable tools for the enhancing the development efficiency and automation in the any Java based project.


Next Article
Article Tags :
Practice Tags :

Similar Reads