[英]Usage of 'pull' command in Jgit
我是 git 的新用户,正在使用JGit与远程 git 存储库进行交互。 在 JGit 中,我最初使用CloneCommand
来克隆一个 repo,它可以正常工作。 但是,当我尝试使用与 SVN 更新 AFAIK 等效的PullCommand
时,本地 repo 内容不会更新。
这是我使用的代码:
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();
}
这不会为我使用命令行推送到远程存储库的新文件更新本地存储库。 但是,如果我删除本地存储库并再次进行克隆,则会反映所有更改。
请让我知道在 JGit 中使用PullCommand
的正确方法是什么。
编辑:
远程仓库的结构:
root ____ file_1
|______ directory_1
|__________ file_2
|__________ file_3
directory_1 和这两个文件是在初始克隆后从命令行推送的,我尝试了这段代码,以便它反映在本地存储库中,这没有发生。
用于克隆存储库的代码:
File file = new File(localPath);
CloneCommand cloneCmd = git.cloneRepository();
try {
cloneCmd.setURI(remotePath)
.setDirectory(file)
.call();
} catch (GitAPIException e) {
e.printStackTrace();
}
在这里, git
、 localPath
和remotePath
是与上面相同的变量。
我怀疑问题是当前分支没有上游配置(因此 pull 不会合并获取的分支)。
要查看在拉取期间发生了什么,请检查pullCmd.call()
的结果:
PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus(); // this should be interesting
文档说明了Git
类的构造函数如下:
构造一个新的 Git 对象,该对象可以与指定的 git 存储库交互。 此类方法返回的所有命令类将始终与此 git 存储库交互。
因此,正如我可以建议的那样,您必须将远程仓库的路径传递给构造函数,现在您正试图从本地仓库中提取。
我遇到了同样的问题。 对我来说,解决方案是在克隆后设置 git 配置文件:
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI("<repo-uri>");
cloneCommand.setDirectory("<repo-dir>");
cloneCommand.call();
Git git = Git.open("<repo-dir>");
StoredConfig config = git.getRepository().getConfig();
config.setString("branch", "master", "merge", "refs/heads/master");
config.setString("branch", "master", "remote", "origin");
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
config.setString("remote", "origin", "url", "<repo-uri>");
config.save();
拉动时,我将远程分支名称设置为“master”,将远程分支名称设置为“origin”:
PullCommand pull = git.pull();
pull.setRemote("origin");
pull.setRemoteBranchName("master");
通过拉动后的这些更改,我看到了本地反映的更改。
从私有存储库尝试此代码用于 git pull
try {
Repository localRepo = new FileRepository(localPath + "/.git");
Git git = new Git(localRepo);
CredentialsProvider cp = new UsernamePasswordCredentialsProvider(userName, pass);
git.pull().setCredentialsProvider(cp).call();
git.close();
} catch (Exception e) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.