使用JGit - Bare Repo提交和推送到GitHub



今天我注册了github,并使用下面描述的技术将现有的文件系统转换为git仓库:

http://crashingdaily.wordpress.com/2009/09/02/initing-a-new-remote-git-repository-with-existing-files/

最重要的(我认为)是这一行:

git --bare init

然后我按照github.com的其余设置教程(这是其中的一部分),并完成。现有的文件系统在Dropbox中,所以我在另外两台使用该文件系统(现在是git repo)的机器上执行了相同的设置。

今晚我试图让JGit添加一个文件,提交它,然后推它。以下是代码的要点,直到它中断:

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)

现在,我不是说这是不合理的主张,因为事实上,我不是版本控制的最好的朋友。我知道一个裸回购是一个只有git,没有其他文件,但在我看来,现在它有文件。我已经手动添加,提交和推送到github使用git从终端。所以我不能马上明白为什么它甚至不能识别回购。

人吗?

编辑-澄清一下,如果有人能提出另一个解决方案,取消这个回购没什么大不了的。我想要一个git repo使用文件系统在我的dropbox,并能够通过Java提交到github。

对于您的代码,我会说选项setGitdir()和findGitDir()不应该同时使用。要检索现有的存储库,我使用findGitDir(新文件("路径/到/my/repo")),这就足够了。

这听起来像是将文件添加到裸存储库中。裸存储库不应该被触摸,除非通过git push和pull命令(或者一般的git命令)。作为指导,我从不查看我的裸存储库。

应该用作中心位置。一旦你创建了git裸repo,你应该克隆它,然后在克隆的基础上对它进行操作,从克隆中推拉。

$ cd /dropbox/repo
$ git init --bare
$ cd /workdir
$ git clone file:///dropbox/repo
$ add files in here
$ git add .
$ git commit -m "initial version"
$ git push origin master
$ more changes here
$ git add etc.

这个和github之间的区别是git克隆,它来自不同的地方。老实说,除非你有一个很好的理由要有一个本地副本,否则我会忘记dropbox的repo,只使用github。

在.build()之后的第三行代码;应该是:

repository.create();

这相当于"git init"命令

在您的情况下,由于您在现有目录中工作,您可能会发现使用Git.open()更方便。

Git git = Git.open(new File(".git"));
System.out.println("Repository: " + git.getRepository().toString());
//Do some git action for instance:
RevCommit rev = git.commit().setAmend(true)
               .setAuthor("me", "me@mail.com")
               .setMessage("Testing commit from jGit").call();
git.close();

来源:r diger Herrmann在Code Affine中撰写的文章

最新更新