JGit 通过 .git-file 连接到单独的 git 存储库
JGit connect to separate git repository by .git-file
我使用了 "git init --separate-git-dir=C:/repo/.git"
并为存储库定义了另一个位置。
我的工作地点是 "C:/work/"
.
在工作地点 git 创建一个 .git-file
并链接到回购地点。
当我使用 JGit 时,我无法连接到我的存储库:
Git
.open(new File("C:\work\.git"))
.reset()
.setMode(ResetType.HARD)
.call();
然后我得到一个异常:
Exception in thread "main" org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: C:\work\.git
at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:582)
at org.eclipse.jgit.api.Git.open(Git.java:117)
at org.eclipse.jgit.api.Git.open(Git.java:99)
at de.test.git.App.main(App.java:20)
如何连接到我的存储库?
感谢您的帮助。
编辑:
感谢 Joni 为我的问题找到了解决方案。
Thank you Joni! You are awesome! I tried it like you wrote with
setting the work tree and it works like a charm. By the way i found
an option to do this steps without to know where the repo location is.
For those who are interesed:
Repository repo = new FileRepositoryBuilder()
.findGitDir(new File("C:\work"))
.setWorkTree(new File("C:\work"))
.build();
Git git = new Git(repo);
git
.reset()
.setMode(ResetType.HARD)
.call();
git.close();
JGit 似乎(还)不支持 --separate-git-dir
选项。
作为work-around,您可以直接打开链接的仓库:
Git
.open(new File("C:/repo/.git"))
.reset()
.setMode(ResetType.HARD)
.call();
像这样使用时,JGit 不会知道您的工作目录在哪里,所以我想任何与工作目录有关的事情都会失败。按照 the user guide,您可以创建自定义的 Repository
对象,如下所示:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("C:/repo/.git"))
.setWorkTree(new File("C:/work"))
.readEnvironment() // scan environment GIT_* variables
.build();
然后使用 Git(Repository)
构造函数代替 Git.open
:
Git git = new Git(repository);
git.reset()
.setMode(ResetType.HARD)
.call();
我使用了 "git init --separate-git-dir=C:/repo/.git"
并为存储库定义了另一个位置。
我的工作地点是 "C:/work/"
.
在工作地点 git 创建一个 .git-file
并链接到回购地点。
当我使用 JGit 时,我无法连接到我的存储库:
Git
.open(new File("C:\work\.git"))
.reset()
.setMode(ResetType.HARD)
.call();
然后我得到一个异常:
Exception in thread "main" org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: C:\work\.git
at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:582)
at org.eclipse.jgit.api.Git.open(Git.java:117)
at org.eclipse.jgit.api.Git.open(Git.java:99)
at de.test.git.App.main(App.java:20)
如何连接到我的存储库?
感谢您的帮助。
编辑:
感谢 Joni 为我的问题找到了解决方案。
Thank you Joni! You are awesome! I tried it like you wrote with setting the work tree and it works like a charm. By the way i found an option to do this steps without to know where the repo location is. For those who are interesed:
Repository repo = new FileRepositoryBuilder()
.findGitDir(new File("C:\work"))
.setWorkTree(new File("C:\work"))
.build();
Git git = new Git(repo);
git
.reset()
.setMode(ResetType.HARD)
.call();
git.close();
JGit 似乎(还)不支持 --separate-git-dir
选项。
作为work-around,您可以直接打开链接的仓库:
Git
.open(new File("C:/repo/.git"))
.reset()
.setMode(ResetType.HARD)
.call();
像这样使用时,JGit 不会知道您的工作目录在哪里,所以我想任何与工作目录有关的事情都会失败。按照 the user guide,您可以创建自定义的 Repository
对象,如下所示:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("C:/repo/.git"))
.setWorkTree(new File("C:/work"))
.readEnvironment() // scan environment GIT_* variables
.build();
然后使用 Git(Repository)
构造函数代替 Git.open
:
Git git = new Git(repository);
git.reset()
.setMode(ResetType.HARD)
.call();