0

I have done the following (in order to learn git rm).

Created an empty repository on an empty folder. Created a file called test.txt, modify it and then run the following (with the consequent results).

git rm test.txt -> `fatal: pathspec 'test.txt' did not match any files`

git add test.txt && git rm test.txt -> 
error: 'test.txt' has changes staged in the index
                   (use --cached to keep the file, or -f to force removal)

So, my question is:

When would git rm test.txt be actually useful?

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296

1 Answers1

1

When would git rm test.txt be actually useful?

When you have already added and committed the file to your repository. The docs tell us that git rm is used to:

Remove files from the index, or from the working tree and the index.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • What's the difference between working tree and index? I have always heard working directory, staging area and git directory (repository). – Hommer Smith Feb 12 '14 at 04:30
  • 1
    Most of the time, if you haven't commited the file and you want to "undo" a `git add` then you will just use `git reset`. There is one exception, though: [`git rm --cached` is the only way to undo `git add` if your repository has no commits yet.](http://stackoverflow.com/a/682343/139010) – Matt Ball Feb 12 '14 at 04:31
  • Google is your friend: http://stackoverflow.com/q/4084921 and http://schacon.github.io/gitbook/7_the_git_index.html – Matt Ball Feb 12 '14 at 04:32
  • Well, if the index is my staging area, then ``git rm`` would remove the files from there, but instead (as in my original question) I get this when trying so: ``error: 'test.txt' has changes staged in the index`` – Hommer Smith Feb 12 '14 at 04:33
  • It's just a warning. Did you not try `git rm -f text.txt`? – Matt Ball Feb 12 '14 at 04:51
  • Yes, I tried that. But my question is still there. Why if git rm is supposed to remove files from my staging area, does not do it? – Hommer Smith Feb 12 '14 at 04:53
  • `git rm` does remove files from the staging area. Why do you think it doesn't? – Matt Ball Feb 12 '14 at 05:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47282/discussion-between-hommer-smith-and-matt-ball) – Hommer Smith Feb 12 '14 at 05:12