Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get modified files from locally cloned Git repository using Java

Tags:

java

git

jgit

In a cloned git repository, I want to pick only the files that are modified (i.e, files that are ready to commit or which are shown as 'modified' if I run the command 'git status'). I do not want to do it on date change comparison as files could have been modified on any day over a period of time.

I need the collection of file names with their absolute file paths.

Is there any such git utility in Java available? Or what will be the better approach?

like image 403
JavaYouth Avatar asked Jun 24 '26 10:06

JavaYouth


1 Answers

import java.io.File;
import java.util.Set;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;

public class GitModifiedFileExtractor {

    public static void main(String[] args) throws IllegalStateException, GitAPIException {
        Git myGitRepo = Git.init().setDirectory(new File("C:\\myClonedGitRepo")).call();
        Status status = myGitRepo.status().call();
        Set<String> modifiedFiles = status.getModified();
        for (String modifiedFile : modifiedFiles) {
            System.out.println("Modified File - " + modifiedFile);
        }
    }
    // Similarly we can get files - added, missing, removed, untracked, etc., 
    // from status object.
}

like image 143
JavaYouth Avatar answered Jun 27 '26 00:06

JavaYouth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!