17

How to use JGit to get the URL of the origin remote?

I am using JGit and I want to execute

git config --get remote.origin.url

How to do that?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
hannsworst
  • 291
  • 2
  • 11

1 Answers1

23

The configuration of a git repository can be accessed through Repository::getConfig(). The returned type is a StoredConfig.

In order to get the URL of the origin remote, use this snippet

String url = repository.getConfig().getString("remote", "origin", "url");

The class ConfigConstants lists a set of frequently used section names and value names.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Is there any documentation on what values are possible for getString()? – Naxos84 Apr 24 '17 at 20:09
  • See the [git config](https://git-scm.com/docs/git-config) documentation for syntax and the configuration settings understood by native Git. However, JGit does not understand all of these. in doubt, you would need to verify if JGit can handle a certain setting or not. – Rüdiger Herrmann Apr 25 '17 at 05:32