git Jgit中'pull'命令的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13399990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:45:29  来源:igfitidea点击:

Usage of 'pull' command in Jgit

javagitgit-pulljgit

提问by Izza

I'm a new user of git and am using JGitto interact with a remote git repository. In JGit, I used CloneCommandto 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.

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

This is the code that I used:

这是我使用的代码:

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.

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

Please let me know what is the correct approach of using PullCommandin JGit.

请让我知道PullCommand在 JGit中使用的正确方法是什么。

EDIT:

编辑:

The structure of the remote repository:

远程仓库的结构:

root ____ file_1
  |______ directory_1
              |__________ file_2 
              |__________ file_3

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.

directory_1 和这两个文件是在初始克隆后从命令行推送的,我尝试了此代码,以便将其反映在本地存储库中,但这种情况并未发生。

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();  
}

Here, git, localPathand remotePathare the same variable as above.

这里,gitlocalPathremotePath是与上面相同的变量。

回答by robinst

I suspect the problem is that the current branch has no upstream configuration (and so pull won't merge the fetched branch).

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

To see what happened during the pull, inspect the result of pullCmd.call():

要查看拉取过程中发生了什么,请检查以下结果pullCmd.call()

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

回答by bsiamionau

Documentations says about constructor of Gitclass following:

文档说明了Git以下类的构造函数:

Constructs a new Git object which can interact with the specified git repository. All command classes returned by methods of this class will always interact with this git repository.

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

So, as I can suggest, you have to pass path of remote repo to constructor, now you're trying to pull from your local repo.

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

回答by Amos Joshua

I ran into the same issue. For me the solution was to set the git config file after cloning:

我遇到了同样的问题。对我来说,解决方案是在克隆后设置 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();

When pulling I set the remote branch name to "master" and the remote to "origin":

拉动时,我将远程分支名称设置为“master”,将远程分支名称设置为“origin”:

PullCommand pull = git.pull();
pull.setRemote("origin");
pull.setRemoteBranchName("master");

With these changes after pulling I see the changes reflected locally.

通过拉动后的这些更改,我看到了本地反映的更改。