218

On SVN, removing something from the filesystem directly (rather than using svn) created a load of headaches.

I haven't found this to be an issue when using git, but I notice that git has it's own rm implementation (git rm).

What is the difference between rm and git rm?

Mus
  • 6,790
  • 22
  • 78
  • 117
cjm2671
  • 16,836
  • 28
  • 92
  • 144

8 Answers8

306

If you just use rm, you will need to follow it up with git add <fileRemoved>. git rm does this in one step.

You can also use git rm --cached which will remove the file from the index (staging it for deletion on the next commit), but keep your copy in the local file system.

Andy
  • 41,796
  • 13
  • 68
  • 68
  • 2
    Good answer. I was able to use `git reset --hard` and then do a checkout of master to get out of detached head state because I knew I didn't have any uncommitted changes. If you haven't committed your changes you may want to do a git stash, but I'm relatively new to git so I don't know the exact command. If you have come here in or after 2014, I hope this answer was useful to you. – Eric Hepperle - CodeSlayer2010 Nov 24 '14 at 19:14
  • @Andy - If I used `rm` (instead of `git rm`) and deleted many files using it, and now don't want `git` to track those. Is there a way to revert changes? I want `git` to tread my `rm` as removed since I didn't use `git rm` in the first place. – Chetan Arvind Patil Jan 30 '18 at 17:43
  • It would be nice if this were clearly spelled out in the docs online (it may be there, but it's obviously not obvious or this question wouldn't have been asked). It would be nice for users to know that they're not doing anything wrong if they go the OS rm route. And there are reasons to go the OS route. E.g. if you need to delete a file to test that something works before committing the change, the OS route is a bit safer than using git rm, as in the git case you have to unstage the action, whereas in the OS option you just have to fetch (I believe). – bob Apr 23 '20 at 21:12
14

Removing files using rm is not a problem per se, but if you then want to commit that the file was removed, you will have to do a git rm anyway, so you might as well do it that way right off the bat.

Also, depending on your shell, doing git rm after having deleted the file, you will not get tab-completion so you'll have to spell out the path yourself, whereas if you git rm while the file still exists, tab completion will work as normal.

hammar
  • 136,420
  • 17
  • 297
  • 381
8

git rm will remove the file from the index and working directory ( only index if you used --cached ) so that the deletion is staged for next commit.

manojlds
  • 275,671
  • 58
  • 453
  • 409
7

Adding to Andy's answer, there is additional utility to git rm:

  1. Safety: When doing git rm instead of rm, Git will block the removal if there is a discrepancy between the HEAD version of a file and the staging index or working tree version. This block is a safety mechanism to prevent removal of in-progress changes.

  2. Safeguarding: git rm --dry-run. This option is a safeguard that will execute the git rm command but not actually delete the files. Instead it will output which files it would have removed.

Joe
  • 2,188
  • 1
  • 21
  • 32
5

However, if you do end up using rm instead of git rm. You can skip the git add and directly commit the changes using:

git commit -a

Struggler
  • 632
  • 2
  • 8
  • 22
3

Remove files from the index, or from the working tree and the index. git rm will not remove a file from just your working directory.

Here's how you might delete a file using rm -f and then remove it from your index with git rm

$ rm -f index.html
$ git status -s
 D index.html
$ git rm index.html
rm 'index.html'
$ git status -s
D  index.html

However you can do this all in one go with just git rm

$ git status -s
$ git rm index.html
rm 'index.html'
$ ls
lib vendor
$ git status -s
D  index.html
basicxman
  • 2,087
  • 14
  • 21
2

When using git rm, the removal will part of your next commit. So if you want to push the change you should use git rm

keis
  • 113
  • 7
0

git rm is safer than rm in some cases when user is using case-insensitive system like Windows or MacOS. The followings are a certain examples.

Let's say that your git repository on case-insensitive system has a committed file named foo.js and you will be running into a error when running git rm Foo.js. But not with rm Foo.js instead.

liuliang
  • 322
  • 1
  • 3
  • 13