cjm2671
cjm2671

Reputation: 19476

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

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 its own rm implementation (git rm).

What is the difference between rm and git rm?

Upvotes: 256

Views: 158827

Answers (9)

Ali
Ali

Reputation: 41

I guess I finally get it

If you make changes to a file file_a.txt

and then do

git add file_a.txt
rm file_a.txt
git commit -m 'Commit message'

Although the file is removed from the working directory (i.e from disk, or moved to the trash bin), it is not removed from the staging area, therefore the changes in the staging area (i.e index) will be committed

if instead you do

git add file_a.txt
git rm file_a.txt
git commit -m 'Commit message'

The file will be deleted from disk and from the staging area, so changes will not be committed

Now if after your last commit, you do either

rm file_a.txt
git commit -m 'Commit message'

or

git rm file_a.txt
git commit -m 'Commit message'

It should make no difference, because you did not add any changes to the staging area or index, so there is only on copy of the file in the working directory, so both command will have the same impact

So in summary rm will just remove from disk, not from staging area git rm will remove from both disk and staging area (a special git area that you cannot reach by regular windows or linux commands)

Upvotes: 4

liuliang
liuliang

Reputation: 422

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.

Upvotes: 0

Joe
Joe

Reputation: 2452

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.

Upvotes: 10

Struggler
Struggler

Reputation: 682

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

Upvotes: 6

basicxman
basicxman

Reputation: 2115

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

Upvotes: 4

Andy
Andy

Reputation: 46404

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.

Upvotes: 346

manojlds
manojlds

Reputation: 301147

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.

Upvotes: 10

hammar
hammar

Reputation: 139840

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.

Upvotes: 15

keis
keis

Reputation: 113

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

Upvotes: 2

Related Questions