在Jgit中使用'pull'命令 [英] Usage of 'pull' command in Jgit
问题描述
我是git的新用户,并且正在使用 JGit 与远程git存储库进行交互.在JGit中,我最初使用CloneCommand
来克隆存储库,并且可以正常工作.但是,当我尝试使用PullCommand
(相当于SVN更新AFAIK)时,本地存储库内容不会更新.
I'm a new user of git and am using JGit to interact with a remote git repository. In JGit, I used CloneCommand
to initially to clone a repo, and it worked without a issue. However, when I try to use PullCommand
, which is the equivalent of SVN update AFAIK, the local repo contents are not updated.
这是我使用的代码:
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();
}
这不会为我使用命令行推送到远程存储库的新文件更新本地存储库.但是,如果删除本地存储库并再次进行克隆,则会反映所有更改.
This doesn't update the local repository for new files which I have pushed to the remote repository using the command line. However, if I delete the local repository and take a clone again, all the changes are reflected.
请让我知道在JGit中使用PullCommand
的正确方法.
Please let me know what is the correct approach of using PullCommand
in JGit.
远程存储库的结构:
root ____ file_1
|______ directory_1
|__________ file_2
|__________ file_3
directory_1以及两个文件在初始克隆后都从命令行推送,我尝试了这段代码,以便将其反映在本地存储库中,这是没有发生的.
directory_1 and the two files are pushed from the commandline after the initial cloning and I tried this code so that it will get reflected in the local repository, which is not happening.
用于克隆存储库的代码:
The code used to clone the repository:
File file = new File(localPath);
CloneCommand cloneCmd = git.cloneRepository();
try {
cloneCmd.setURI(remotePath)
.setDirectory(file)
.call();
} catch (GitAPIException e) {
e.printStackTrace();
}
此处,git
,localPath
和remotePath
与上面的变量相同.
Here, git
, localPath
and remotePath
are the same variable as above.
推荐答案
我怀疑问题在于当前分支没有上游配置(因此pull不会合并获取的分支).
I suspect the problem is that the current branch has no upstream configuration (and so pull won't merge the fetched branch).
要查看拉动过程中发生了什么,请检查pullCmd.call()
的结果:
To see what happened during the pull, inspect the result of pullCmd.call()
:
PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus(); // this should be interesting
这篇关于在Jgit中使用'pull'命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!