我如何使用rugged来创建和提交命令行中的文件?

我正在尝试使用坚固的做一些非常简单的事情:创建并提交一个文件,使存储库处于与执行相同的状态:

git init echo "blah blah blah" > blah.txt git add blah.txt git commit -m "write blah.txt" 

留下git status打印

 On branch master nothing to commit, working directory clean 

我使用的代码改编自坚固的回购自述文件 ,归结为:

 name = "blah.txt" repo = Rugged::Repository.init_at dir File.open File.join(dir, name), 'w' do |f| f.write content end oid = Rugged::Blob.from_workdir repo, name index = repo.index index.add(:path => name, :oid => oid, :mode => 0100644) options = {} options[:tree] = index.write_tree(repo) options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now } options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now } options[:message] = "write #{ name }" options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact options[:update_ref] = 'HEAD' commit = Rugged::Commit.create(repo, options) 

这似乎使存储库处于正确的状态,但工作目录在一个奇怪的地方, git status打印

 On branch master Changes to be committed: (use "git reset HEAD ..." to unstage) deleted: blah.txt Untracked files: (use "git add ..." to include in what will be committed) blah.txt 

我不明白git在这一点上认为发生了什么( blah.txt是为删除而上演的?)。 执行git reset --hard将工作目录恢复到我所知道的所需状态。

在此先感谢您的帮助。

您忘记将更改保存到索引中。 您已更新引用以指向新提交,但索引未更改(因为您从未调用Index#write ),因此就git而言,删除是暂存的,因为该文件不在索引中,就像如果你运行了git rm blah.txt