今天我注册了 github,并使用此处描述的技术将现有文件系统转换为 git 存储库:
最重要的(我认为)它涉及这一行:
git --bare init
然后我遵循了 github.com 的其余设置教程(这是其中的一部分)并完成了。现有的文件系统位于 Dropbox 中,因此我在使用该文件系统(现在是 git 存储库)的另外两台机器上执行了相同的设置。
今晚我试图让 JGit 添加一个文件,提交它然后推送它。这是代码的 Gist ,直到它中断为止:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("path/to/my/repo"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
Git git = new Git(repository);
AddCommand add = git.add();
try{
add.addFilepattern("PlayState.as").call();`
顺便说一下,这基本上是从 JGit 教程中逐字记录的。它在最后引用的那一行抛出异常并指出:
org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:838)
at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:886)
at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:136)
at flipa.FLIPAGame.writeToFlixel(FLIPAGame.java:77)
at flipa.FLIPAGame.main(FLIPAGame.java:58)
现在,我并不是说这样说是不合理的,因为说实话我不是版本控制的最佳 friend 。我知道一个裸仓库是一个只有 git 而没有其他文件的仓库,但在我看来,现在它里面有文件。我已经从终端使用 git 手动添加、提交并推送到 github。所以我无法立即明白为什么它甚至无法识别 repo 协议(protocol)。
有人要吗?
编辑 - 需要澄清的是,如果有人可以提出另一种解决方案,那么终止这个 repo 没什么大不了的。我想要一个 git 存储库来使用我的保管箱中的文件系统,并能够通过 Java 提交到 github。
最佳答案
对于您的代码,我认为不应同时使用选项 setGitdir() 和 findGitDir()。 要检索现有存储库,我使用 findGitDir(new File("path/to/my/repo") 就足够了。
关于java - 使用 JGit - Bare Repo 提交并推送到 GitHub?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8234373/