【发布时间】:2014-10-03 13:33:42
【问题描述】:
我正在尝试使用 jgit 从 git 存储库中提取更改。
我在拉动时遇到 HEAD DETACHED 错误的小问题
我已经在 stackoverflow 上阅读了其他答案。尝试这些解决方案似乎没有帮助。
我有 2 个远程分支 1. 分期 2. 大师
这里是示例代码:
public static void main(String [] args){
final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;
try{
//setting up local repository as just testNewLiferRepo
String repoName = localRepositoryPath + "testNewLiferRepo";
File f = new File(repoName);
Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
ObjectId oldObjid = null;
Git git = null;
if (f.exists()) {
oldObjid = localRepo.resolve(Constants.HEAD);
git = Git.open(f);
git.pull().call();
ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
git.getRepository().close();
} else {
git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
if(!localRepo.getBranch().equalsIgnoreCase("staging")){
git.checkout().setName("refs/remotes/origin/staging").setForce(true).call();
}
git.getRepository().close();
}
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RevisionSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmbiguousObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您可以更新存储库上的 testXML 文件中的版本来测试拉取。
任何指针都非常感谢
谢谢
【问题讨论】:
-
请链接您已经阅读过的其他解决方案,以便其他人知道您已经尝试过哪些解决方案。
标签: jgit