Java 如何使用jgit将现有存储库克隆到新的github实例?
我们有两个独立的GitHub实例正在运行。一个GitHub实例是Java 如何使用jgit将现有存储库克隆到新的github实例?,java,git,github,jgit,Java,Git,Github,Jgit,我们有两个独立的GitHub实例正在运行。一个GitHub实例是https://github.dev.host.com和其他github实例是https://github.host.com。我在https://github.dev.host.com我需要将其迁移到这个新的github实例https://github.host.com 我使用JGit是因为我在使用Java。例如-下面是https://github.dev.host.com需要迁移到新github实例的实例https://githu
https://github.dev.host.com
和其他github实例是https://github.host.com
。我在https://github.dev.host.com
我需要将其迁移到这个新的github实例https://github.host.com
我使用JGit是因为我在使用Java。例如-下面是https://github.dev.host.com
需要迁移到新github实例的实例https://github.host.com
https://github.dev.host.com/Database/ClientService
https://github.dev.host.com/Database/Interest
由于我正在使用JGit,所以我想通过Java代码在我的新GitHub实例中创建上述两个存储库。在运行Java代码之后,我应该可以从我的https://github.dev.host.com
进入我的新Github实例https://github.host.com
如下所示-
https://github.host.com/Database/ClientService
https://github.host.com/Database/Interest
我只需要迭代旧github实例中的所有存储库列表,如果它们没有在新github实例中与其所有内容和分支一起退出,则创建它们。如果它们已经存在,则覆盖从旧实例到新实例的所有更改
使用JGit可以做到这一点吗?我还可以通过我的用户名和密码https
访问我的github实例
到目前为止,我只能做一些基本的东西,如下所示,这是我通过教程学到的
public class CreateNewRepository {
public static void main(String[] args) throws IOException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
// create the directory
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
repository.create();
System.out.println("Having repository: " + repository.getDirectory());
repository.close();
FileUtils.deleteDirectory(localPath);
}
}
任何建议都会有很大帮助,因为这是我第一次使用JGit 一种可行的方法是将repositoy从源服务器克隆到一个临时位置,然后从那里将其推送到目标服务器 您可以使用JGit克隆存储库,如下所示:
Git.cloneRepository()
.setCredentialsProvider( new UsernamePasswordCredentialsProvider( "user", "password" ) );
.setURI( remoteRepoUrl )
.setDirectory( localDirectory )
.setCloneAllBranches( true )
.call();
要将刚刚克隆的repositoy传输到目标服务器,必须首先在目标服务器上创建一个存储库。JGit和Git都不支持这个步骤。GitHub提供了一个RESTAPI,让您可以使用它。还列出了可用于此API的语言绑定,其中包括Java
存储库(空)存在后,您可以从临时副本推送到远程:
Git git = Git.open( localDirectory );
git.push()
.setCredentialsProvider( new UsernamePasswordCredentialsProvider( "user", "password" ) );
.setRemote( newRemoteRepoUrl )
.setForce( true )
.setPushAll()
.setPushTags()
.call()
可以找到有关身份验证的更多信息
请注意,如果源存储库包含标记,则这些标记必须在克隆后单独提取到临时存储库中。谢谢您的帮助。现在我知道我需要做什么了。所以我想最好的方法是,将上述两个存储库克隆到一个临时位置,然后从那里将其推送到目标服务器,这就是我的新github实例?“我的理解正确吗?”大卫:是的,我的回答是这样的。还有一件事,在您的第二个代码片段中,它将
localDirectory
更改推送到目标。在我的例子中,目标是这个新的github实例https://github.host.com
但是第二个代码片段如何知道我需要将localDirectory
更改推送到新的github实例?恐怕我不太理解这个问题。指定给Git.open()
的目录指定了存储库。git
变量的作用类似于命令的工厂,其push()
方法返回在localRepository
上运行的PushCommand
。你可以找到更多关于使用JGit访问Repositories的信息。如果我之前不清楚,很抱歉。我的意思是-我在这个github实例中有一个存储库https://github.dev.host.com
我需要将其迁移到这个新的Github实例https://github.host.com
。因此,我使用您的第一个代码片段从我的第一个github实例克隆存储库,但是在您的第二个代码片段中,它如何知道我需要将localDirectory更改推送到我的新github实例(https://github.host.com
)。我已经用代码更新了这个问题,我必须让它更清楚我想问什么。