Ruby 关于砂砾的几个问题

Ruby 关于砂砾的几个问题,ruby,git,grit,Ruby,Git,Grit,我有一些关于Grit/Git的问题,希望您能帮助我。这是我的密码: # create Repo r = Repo.init_bare 'myrepo.git' i = r.index # first commit to master i.add('myfile.txt', 'my file contents') i.commit("This is my commit") # second commit to master i.read_tree("master") i.add('myfile

我有一些关于Grit/Git的问题,希望您能帮助我。这是我的密码:

# create Repo
r = Repo.init_bare 'myrepo.git'
i = r.index

# first commit to master
i.add('myfile.txt', 'my file contents')
i.commit("This is my commit")

# second commit to master
i.read_tree("master")
i.add('myfile2.txt', 'my file 2 contents')
i.commit("This is my second commit", [r.commits.first])

# first commit to newbranch
i.read_tree("master")
i.add('myfile3.txt', 'my file 3 contents')
i.commit("This is my third commit", [r.commits.first], nil, nil, 'newbranch')

# second commit to newbranch
i.read_tree("newbranch")
i.add('myfile4.txt', 'my file 4 contents')
i.commit("This is my fourth commit", [r.commit("newbranch")], nil, nil, 'newbranch')
使用这段代码,我试图创建一个repo,向master提交两次,从master创建一个新分支,并向这个分支提交两次。但问题是,当我这样做时:

r.commits("newbranch")  # => 3
上面说“newbranch”上只有3次提交。它省略了master上的第二个提交。为什么呢?我的代码有问题吗

我最困惑的是如何在分支时指定父提交,以及在“newbranch”上进行第二次提交时指定父提交

希望你能帮忙。谢谢

只要做:

r、 结帐(右分行(“纽伯兰”))


然后进行提交。

API.txt文件指出,这尚未作为本机库实现实现。但是,您仍然可以通过以下方式绕过任何库实现来执行此操作:

r.git.native :checkout, {}, 'my_branch'

查看Grit源的git.rb了解更多详细信息。

现在,这是我提交给特定分支的解决方案:

index = repo.index
index.read_tree( branch )
index.add( path_to_a_file, file_data )
c = repo.commits( branch ) 
index.commit( comment, repo.commits( branch ), nil, nil, branch )

最后一行允许提交到特定分支,而无需签出它。此外,需要调用read_tree函数,除非您不想丢失索引。

我在Grit文档中看不到这一点,我也不需要该函数?我该如何作出承诺?通过索引?要创建分支,请使用
r.git.native:checkout,{b:true},'my_branch'