A few questions about Grit
I have a few questions about Grit/Git that I hope you can help me with. Here's my code:
# 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')
With this code I'm trying to create a repo, commit twice to master, create a new 开发者_运维技巧branch from master, and commit twice to this branch. But the problem is that when I do this:
r.commits("newbranch") # => 3
It says that there's only 3 commits on "newbranch". It leaves out the second commit on master. Why is that? Is there something wrong with my code?
The thing I'm most confused with is how to specify the parent commit when branching out, and when making the second commit on the "newbranch".
Hope you can help. Thanks
The API.txt file says that this is not yet implemented as a native library implementation. However, you can still do this, bypassing any library implementation, with:
r.git.native :checkout, {}, 'my_branch'
Look into git.rb of the Grit source for more details.
For now, this is my solution committing to a specific branch:
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 )
The last line is allowing to commit to a specific branch without checking it out. Also, call to read_tree function is required, unless you don't want to lose your index.
Just do:
r.checkout(r.branch('newbranch'))
and then make your commits.
精彩评论