1

This is my code for add remote.

  FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(gitdir)
                    .readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build();
            Git git = new Git(repository);

            StoredConfig config = git.getRepository().getConfig();
            config.setString("remote", "origin", "url", url);
            config.save();

Now how to use fetch command using jGit?

Picks Prabhakar
  • 335
  • 1
  • 3
  • 13

1 Answers1

2

You can use the JGit FetchCommand:

git.fetch().setRemote("origin").call();
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
  • 1
    I am getting this error "org.eclipse.jgit.api.errors.InvalidRemoteException: Invalid remote: origin" – Milinda Kasun Aug 27 '21 at 02:26
  • Hey @MilindaKasun, sorry I don't JGit in my job anymore. Is it possible that your origin is not added to Git/JGit repository correctly? or it is added but its value is invalid? A valid remote should look like `https://github.com/mincong-h/mincong-h.github.io.git`, you can find it on any GitHub project by clicking the "code" button. – Mincong Huang Aug 27 '21 at 20:57