2011-04-30 92 views
2

我有一些關於砂礫/ 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') 

有了這個代碼,我試圖創建一個回購協議,提交兩次主,創建主一個新的分支,並提交兩次該分支。但問題是,當我這樣做:

r.commits("newbranch") # => 3 

它說,只有3提交「newbranch」。它留下了主人的第二次提交。這是爲什麼?我的代碼有問題嗎?

我最混淆是如何指定父提交分支出來的時候,並製作了「newbranch」第二承諾時的事情。

希望你能幫上忙。由於

回答

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函數。

-1

只要做到:

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

,然後進行提交。

+0

我在沙礫文檔中沒有看到這個功能,我的功能也沒有?我將如何進行提交?通過索引? – Ronze 2011-04-30 05:49:23

+0

@Ronze https://github.com/mojombo/grit/blob/master/API.txt – manojlds 2011-04-30 05:55:20

3

的API.txt文件說,這還不是作爲一個機庫實現來實現。但是,你仍然可以做到這一點,繞過任何庫實現,具有:

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

查找到有關詳細信息,砂粒源的git.rb。

+1

要創建一個分支,使用'r.git.native:結賬,{B:真實},「my_branch'' – aceofspades 2012-02-28 17:31:30