Why use 'git rm' to remove a file instead of 'rm'?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

> 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

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

Best way to delete all the files and we can say best way to delete all git activities Use :

rm -rf .git

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncjm2671View Question on Stackoverflow
Solution 1 - GitAndyView Answer on Stackoverflow
Solution 2 - GithammarView Answer on Stackoverflow
Solution 3 - GitmanojldsView Answer on Stackoverflow
Solution 4 - GitJoeView Answer on Stackoverflow
Solution 5 - GitStrugglerView Answer on Stackoverflow
Solution 6 - GitbasicxmanView Answer on Stackoverflow
Solution 7 - GitkeisView Answer on Stackoverflow
Solution 8 - GitliuliangView Answer on Stackoverflow
Solution 9 - GitTanya sharmaView Answer on Stackoverflow