3

I've done this 100s of times. I deleted a file from a directory and then run git status which looks fine.

# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    themes/custom/gitb/templates/views-view-field--field-overall-rating.tpl.php 
#

Then I run git rm themes/custom/gitb/templates/views-view-field--field-overall-rating.tpl.php and receive an error message

error: pathspec 'themes/custom/gitb/templates/views-view-field--field-overall-rating.tpl.php' did not match any file(s) known to git.

git status "knows" about the file but but git rm doesn't and won't remove it. I'm stuck and how do I solve this?

gitb
  • 1,090
  • 2
  • 12
  • 20

3 Answers3

10

A quick and dirty solution would be to add back the file and use

git rm themes/custom/gitb/templates/views-view-field--field-overall-rating.tpl.php

to remove it.

git rm removes the file from the filesystem too, so you don't need (and in general you shouldn't) to manually remove the file.

EDIT

A cleaner way to do so, would be to make git to notice the missing file using

git add -u

or

git commit -a

From the doc of git-add, here's the description of the -u option

Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

and here's the one for the -a option of git-commit

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • touch and remove didn't quite work but touch, add and then remove worked so thanks very much. I need to understand the difference between the "working tree" and the "index". – gitb Jan 21 '13 at 18:32
  • yeah probably the `touch` trick is too naive. I'll remove it from my answer. – Gabriele Petronella Jan 21 '13 at 18:34
1

The git rm man page describes several ways of removing files no longer present in the working tree.

In the future, it is simpler to delete the file with git rm than to delete it separately.

cjc343
  • 3,735
  • 1
  • 29
  • 34
0

this append when you remove the files an directories not using git's git rm command.

I did git stash

It restored the files that I had deleted using rm instead of git rm.

I fact I first did a checkout of the last hash, but I do not believe this is required.

user1767316
  • 3,276
  • 3
  • 37
  • 46