How to update the working directory when creating a commit with Rugged/libgit2?

I’m trying to create a commit with rugged using the following test script:

require "rugged"
r =".")
index = r.index
blob = r.write("My test", :blob)
index.add(:oid => blob, :path => "", :mode => 0100644)
tree = index.write_tree
parents = [r.references["refs/heads/master"].target].compact
actor = {:name => "Actor", :email => "actor@bla"}
options = {
    :tree => tree,
    :parents => parents,
    :committer => actor,
    :message => "message",
    :update_ref => "HEAD"
puts Rugged::Commit.create(r, options)

The commit is created, and the script outputs 773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7 (the SHA of the new commit). The generated commit and tree look like they’re supposed to:

  • Git:Heroku Repository or object not found:
  • GIT repository layout for server with multiple projects
  • Using Git for local development with Subversion on the server?
  • 'git clone' respects umask, except for top level project directory
  • Why is git staging files for my checkin which were picked up in a merge, but untouched by me?
  • Provide a password for the “git push” command in GitPython
  • ludwig$ git cat-file commit 773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7
    tree 253d0a2b8419e1eb89fd462ef6e0b478c4388ca3
    parent bb1593b0534c8a5b506c5c7f2952e245f1fe75f1
    author Actor <actor@bla> 1417735899 +0100
    committer Actor <actor@bla> 1417735899 +0100
    ludwig$ git ls-tree 253d0a2b8419e1eb89fd462ef6e0b478c4388ca3
    100644 blob a7f8d9e5dcf3a68fdd2bfb727cde12029875260b    Initial file
    100644 blob 7a76116e416ef56a6335b1cde531f34c9947f6b2

    However, the working directory is not updated:

    ludwig$ ls
    Initial file   rugged_test.rb
    ludwig$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)

    I have to do a git reset --hard HEAD to get the missing file to show up in the working directory. I thought creating a Rugged commit, and setting :update_ref => "HEAD", was supposed to update the working directory automatically, but something must be going wrong, because doing r.checkout_head also has no effect. However, I think I’m following the rugged examples correctly. What am I missing here?


    ludwig$ gem list rugged
    *** LOCAL GEMS ***
    rugged (0.21.2)

  • Git Fatal Error: info/refs not found
  • Duplicate commits in git branch after rebase / pull request workflow on bitbucket
  • Is it possible to use winmerge to view git diffs from cygwin?
  • What is the difference between `git merge` and `git merge --no-ff`?
  • Few questions with GIT, possible to have custom .gitignore? Read only access?
  • fatal: git-write-tree: error building trees
  • One Solution collect form web for “How to update the working directory when creating a commit with Rugged/libgit2?”

    The steps you’re taking are those for when you do not want to affect the workdir or current branch. You are not creating the file and you are not writing the modified index to disk.

    If you want to put a file on the filesystem and then track it in a new commit, start by creating the file

    # Create the file and give it some content
    f = open("", "w")
    f << "My test"
    # The file from the workdir from to the index
    # and write the changes out to disk
    index = repo.index
    # Get the tree for the commit
    tree = index.write_tree

    and then commit as you are doing now.

    Git Baby is a git and github fan, let's start git clone.