与this question类似,但我没有创建新文件,而是尝试从原点合并。在使用Rugged::Repository
merge_commits
创建新索引以及新的合并提交后,git会将新文件(来自origin
)报告为已删除。
创建合并索引
> origin_target = repo.references['refs/remotes/origin/master'].target
> merge_index = repo.merge_commits(repo.head.target, origin_target)
和新的合并提交,
> options = {
update_ref: 'refs/heads/master',
committer: {name: 'user', email: 'user@foo.com', time: Time.now},
author: {name: 'user', email: 'user@foo.com', time: Time.now},
parents: [repo.head.target, origin_target],
message: "merge `origin/master` into `master`"}
并确保使用合并索引中的树。
> options[:tree] = merge_index.write_tree(repo)
创建提交
> merge_commit = Rugged::Commit.create(repo, options)
检查我们的HEAD是否已更新:
> repo.head.target.tree
=> #<Rugged::Tree:16816500 {oid: 16c147f358a095bdca52a462376d7b5730e1978e}>
<"first_file.txt" 9d096847743f97ba44edf00a910f24bac13f36e2>
<"second_file.txt" 8178c76d627cade75005b40711b92f4177bc6cfc>
<"newfile.txt" e69de29bb2d1d6434b8b29ae775ad8c2e48c5391>
看起来不错。我在索引中看到了新文件。把它写到磁盘上。
> repo.index.write
=> nil
...但是git将新文件报告为已删除:
$ git st
## master...origin/master [ahead 2]
D newfile.txt
如何正确更新索引和工作树?
答案 0 :(得分:1)
Git存储库和工作目录之间存在重要区别。虽然大多数常见的命令行git命令在工作目录和存储库上运行,但libgit2 / librugged的低级命令主要仅在存储库上运行。这包括在示例中编写索引。
要更新工作目录以匹配索引,以下命令应该起作用(在写入索引之后):
options = { strategy: force }
repo.checkout_head(options)
checkout_head的文档:http://www.rubydoc.info/github/libgit2/rugged/Rugged/Repository#checkout_head-instance_method
注意:我使用update_ref: 'HEAD'
进行了提交测试。我不确定update_ref: 'refs/heads/master'
是否会产生同样的效果。