2

我有几个关于 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”上进行第二次提交时。

希望你能帮忙。谢谢

4

3 回答 3

3

API.txt 文件说这还没有作为本机库实现。但是,您仍然可以这样做,绕过任何库实现,使用:

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

查看 Grit 源的 git.rb 以获取更多详细信息。

于 2012-01-04T14:17:12.793 回答
2

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

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 函数,除非您不想丢失索引。

于 2012-05-28T12:59:04.670 回答
-1

做就是了:

r.checkout(r.branch('newbranch'))

然后做出你的承诺。

于 2011-04-30T02:54:02.963 回答