2

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

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 <file>..." to unstage)

    deleted:    blah.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    blah.txt

我不明白 git 认为此时正在发生什么(blah.txt准备删除?)。git reset --hard据我所知,执行将工作目录恢复到所需的状态。

在此先感谢您的帮助。

4

1 回答 1

4

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

于 2014-07-03T16:20:01.010 回答