Question

I know the support for git submodules is limited in JGit, but still was wondering how can I achieve this:

git submodule foreach git checkout <branchName>

and similar commands using JGit.

Or is there any other better Java based API for Git?

Was it helpful?

Solution

In JGit there is a SubmoduleWalk that might help with your problem. To run a checkout command on all submodules, you would write something along these lines:

try (SubmoduleWalk walk = SubmoduleWalk.forIndex(repository)) {
  while(walk.next()) {
    try (Repository submoduleRepository = walk.getRepository()) {
      Git.wrap(submoduleRepository).checkout().call();
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top