在Jgit中使用'pull'命令

Izz*_*zza 7 java git git-pull jgit

我是git的新用户,我正在使用JGit与远程git存储库进行交互.在JGit中,我过去常常CloneCommand克隆一个repo,它没有问题.但是,当我尝试使用时PullCommand,相当于SVN更新AFAIK,本地存储库内容不会更新.

这是我使用的代码:

private String localPath;
private Repository localRepo;
private Git git;

localPath = "/home/test/git_repo_test";
remotePath = "https://github.com/test/repo_1.git";

try {
    localRepo = new FileRepository(localPath + "/.git");
} catch (IOException e) {
    e.printStackTrace();  
}
git = new Git(localRepo);

PullCommand pullCmd = git.pull();
try {
    pullCmd.call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}
Run Code Online (Sandbox Code Playgroud)

这不会更新我使用命令行推送到远程存储库的新文件的本地存储库.但是,如果我删除本地存储库并再次获取克隆,则会反映所有更改.

请告诉我PullCommand在JGit 中使用的正确方法是什么.

编辑:

远程存储库的结构:

root ____ file_1
  |______ directory_1
              |__________ file_2 
              |__________ file_3
Run Code Online (Sandbox Code Playgroud)

在初始克隆之后,从命令行推送了directory_1和两个文件,我尝试了这个代码,这样它就会反映在本地存储库中,而这种情况并没有发生.

用于克隆存储库的代码:

File file = new File(localPath);
CloneCommand cloneCmd = git.cloneRepository();
try {
    cloneCmd.setURI(remotePath)
            .setDirectory(file)
            .call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}
Run Code Online (Sandbox Code Playgroud)

这里,git,localPathremotePath与上述相同的变量.

rob*_*nst 7

我怀疑问题是当前分支没有上游配置(因此pull不会合并获取的分支).

要查看拉动期间发生的情况,请检查以下结果pullCmd.call():

PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus();  // this should be interesting
Run Code Online (Sandbox Code Playgroud)


bsi*_*nau 4

文档介绍了Git以下类的构造函数:

构造一个新的 Git 对象,该对象可以与指定的 git 存储库进行交互。此类的方法返回的所有命令类将始终与此 git 存储库交互。

因此,正如我建议的那样,您必须将远程存储库的路径传递给构造函数,现在您正尝试从本地存储库中提取数据。