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?
You can view that origin with the command git remote -v, which will list the URL of the remote repo.
In Git, "origin" is a shorthand name for the remote repository that a project was originally cloned from. More precisely, it is used instead of that original repository's URL - and thereby makes referencing much easier.
JGit is a lightweight, pure Java library implementation of the Git version control system – including repository access routines, network protocols, and core version control algorithms. JGit is a relatively full-featured implementation of Git written in Java and is widely used in the Java community.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With