使用 JGit API 添加远程 URL
Adding remote URL with the JGit API
有没有一种方法可以使用 JGit API 从远程 URL 克隆,我可以在某个地方更改它默认采用的原点并将其设为 origin2
,如origin
是否配置了另一个遥控器?
您可以使用CloneCommand::setRemote()
更改将要克隆的存储库的远程名称。默认情况下,它被命名为 origin
.
您也可以在添加后通过直接操作Git配置来更改远程名称:
Repository repository = ...;
String oldName = "origin";
String newName = "other-origin";
StoredConfig config = repository.getConfig();
Set<String> names = config.getNames( ConfigConstants.CONFIG_REMOTE_SECTION, oldName );
for( String name : names ) {
String value = config.getString( ConfigConstants.CONFIG_REMOTE_SECTION, oldName, name );
config.setString( ConfigConstants.CONFIG_REMOTE_SECTION, newName, name, value );
}
config.unsetSection( ConfigConstants.CONFIG_REMOTE_SECTION, oldName );
config.save();
要添加新的遥控器,请像这样使用 RemoteAddCommand
:
git.remoteAdd().setName( "new-remote" ).setUri( new URIish( "..." ) ).call();
有没有一种方法可以使用 JGit API 从远程 URL 克隆,我可以在某个地方更改它默认采用的原点并将其设为 origin2
,如origin
是否配置了另一个遥控器?
您可以使用CloneCommand::setRemote()
更改将要克隆的存储库的远程名称。默认情况下,它被命名为 origin
.
您也可以在添加后通过直接操作Git配置来更改远程名称:
Repository repository = ...;
String oldName = "origin";
String newName = "other-origin";
StoredConfig config = repository.getConfig();
Set<String> names = config.getNames( ConfigConstants.CONFIG_REMOTE_SECTION, oldName );
for( String name : names ) {
String value = config.getString( ConfigConstants.CONFIG_REMOTE_SECTION, oldName, name );
config.setString( ConfigConstants.CONFIG_REMOTE_SECTION, newName, name, value );
}
config.unsetSection( ConfigConstants.CONFIG_REMOTE_SECTION, oldName );
config.save();
要添加新的遥控器,请像这样使用 RemoteAddCommand
:
git.remoteAdd().setName( "new-remote" ).setUri( new URIish( "..." ) ).call();