英文:
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找到了我的问题的解决方案。
> 谢谢Joni!你太棒了!我按照你的方法尝试了一下,设置了工作目录,效果非常好。顺便说一下,我找到了一种在不知道仓库位置的情况下执行这些步骤的选项。对于那些感兴趣的人:
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();
英文:
I used "git init --separate-git-dir=C:/repo/.git"
and define another location for the repository.
My work location is "C:/work/"
.
In the work location git creates a .git-file
with linking to the repo location.
When i use JGit i can not connect to my repo:
Git
.open(new File("C:\\work\\.git"))
.reset()
.setMode(ResetType.HARD)
.call();
Then i get an exception:
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)
How to connect to my repository?
Thanks for help.
Edit:
Thanks to Joni who found a solution for my issue.
> 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();
答案1
得分: 1
似乎 JGit 尚不支持 --separate-git-dir
选项。
作为一种解决方法,您可以直接打开链接的存储库:
Git
.open(new File("C:/repo/.git"))
.reset()
.setMode(ResetType.HARD)
.call();
在这种用法下,JGit 将不知道您的工作目录在哪里,所以我想与工作目录有关的任何操作都将失败。根据用户指南,您可以像这样创建自定义的 Repository
对象:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("C:/repo/.git"))
.setWorkTree(new File("C:/work"))
.readEnvironment() // 扫描环境中的 GIT_* 变量
.build();
然后使用 Git(Repository)
构造函数,而不是 Git.open
:
Git git = new Git(repository);
git.reset()
.setMode(ResetType.HARD)
.call();
英文:
It seems that JGit does not (yet) support the --separate-git-dir
option.
As a work-around, you can open the linked repo directly:
Git
.open(new File("C:/repo/.git"))
.reset()
.setMode(ResetType.HARD)
.call();
When used like this, JGit won't know where your working directory is so I imagine anything that has to do with the working directory will fail. Going by the user guide you can create a customized Repository
object like this:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("C:/repo/.git"))
.setWorkTree(new File("C:/work"))
.readEnvironment() // scan environment GIT_* variables
.build();
And then use the Git(Repository)
constructor instead of Git.open
:
Git git = new Git(repository);
git.reset()
.setMode(ResetType.HARD)
.call();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论