May 11

“git add . ” mistakenly deletes a directory which wasn’t touched. Bug?

Posted by mtoledo
Filed under linux | No Comments

I was having some weird issues with git on my pet project:

Basically, git tried to automatically delete my ‘restful_authentication’ plugin everytime I did a “git add .”, even if my working tree was clean.

For instance, after a git pull (fully synchronized with origin):

~/projects/bushi2do master $ git pull
Already up-to-date.
~/projects/bushi2do master $ git status
# On branch master
nothing to commit (working directory clean)
~/projects/bushi2do master $ ls vendor/plugins/
acts_as_list  restful_authentication

Notice my ‘restful_authentication’ is there, untouched.

Now, if I ‘git add .’ :

~/projects/bushi2do master $ git add .
~/projects/bushi2do master $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   vendor/plugins/restful_authentication
#       deleted:    vendor/plugins/restful_authentication/.gitignore
#       deleted:    vendor/plugins/restful_authentication/CHANGELOG
#       deleted:    vendor/plugins/restful_authentication/README.textile
#       deleted:    vendor/plugins/restful_authentication/Rakefile
#       deleted:    vendor/plugins/restful_authentication/TODO
# ...

Notice how he said “new file:” and then “deleted: “. The “deleted: ” entries occur for every file on my restful_authentication plugin and they are already staged. Curiously, if I look into the directory, its still there, untouched as I expected:

# ...
#       deleted:    vendor/plugins/restful_authentication/rails/init.rb
#       deleted:    vendor/plugins/restful_authentication/restful-authentication.gemspec
#       deleted:    vendor/plugins/restful_authentication/tasks/auth.rake
#
~/projects/bushi2do master $ ls vendor/plugins/
acts_as_list  restful_authentication

Curiously enough, if I proceed with the commit and then push to remote, my local copy will still work correctly, because my restful authentication plugin will still be here. But my production server will be broken, because it will be removed from the remote branch.

Oddly enough also, even if I “git reset –hard HEAD” and then “git add .”, the files are still deleted:

~/projects/bushi2do master $ git reset --hard HEAD
HEAD is now at d2d3364 Removing redundant route
~/projects/bushi2do master $ git status
# On branch master
nothing to commit (working directory clean)
~/projects/bushi2do master $ git add .
~/projects/bushi2do master $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   vendor/plugins/restful_authentication
#       deleted:    vendor/plugins/restful_authentication/.gitignore
#       deleted:    vendor/plugins/restful_authentication/CHANGELOG
#       deleted:    vendor/plugins/restful_authentication/README.textile
#       deleted:    vendor/plugins/restful_authentication/Rakefile
#       deleted:    vendor/plugins/restful_authentication/TODO
# ...

Very weird indeed, and I don’t know how to explain this behavior.

This was only fixed by removing the version control on the “restful_authentication” directory:

~/projects/bushi2do master $ rm -rf vendor/plugins/restful_authentication/.git
~/projects/bushi2do master $ git add .
~/projects/bushi2do master $ git status
# On branch master
nothing to commit (working directory clean)

The downside to this fix is that you can no longer ‘git pull’ on the plugin’s directory to grab the updates.

Anyone suffered the same problem using git + restful_authentication or some other plugin? Any other known workaround?

Anyway, I hope this helps people to save some time if they stumble on the same issue.

This entry was posted on Monday, May 11th, 2009 at 11:38 pm and is filed under linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply