-1

I started learning git and I found it strange how you have to use git command git rm file to delete file from repository. I aware that there is another way to do this which is to delete the file straight from repository then use git add file. Why do you have to do this, I thought git is smarter then that? What happen if I just move the file I want to delete to trashcan and process with normal flow git add . then git commit -m "message". I currently working on a school project and that is how I always deleting my files up to this point (move it to trash can and not declare git rm file). I didn't run into any problem or maybe they were problems but other team members may had cleaned it up.

Tam Lam
  • 135
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [Why use 'git rm' to remove a file instead of 'rm'?](http://stackoverflow.com/questions/7434449/why-use-git-rm-to-remove-a-file-instead-of-rm) – Steve Mar 31 '17 at 10:26
  • 1
    I looked at that before asked this question. It doesn't explain why you need to do it and a explanation of why is all I want to know – Tam Lam Mar 31 '17 at 10:29
  • 1
    Although that question doesn't really explain why. `rm` will remove the file from the working directory, while `git rm` will remove it from the index. – Steve Mar 31 '17 at 10:29
  • 1
    When you `git add .` that updates your index and lets git know that you want the file removed. Personally I avoid `git add .` and make heavy use of `git status` because I want to see exactly what I'm about to commit – Steve Mar 31 '17 at 10:35
  • If you use `git status` and you're sure that you haven't accidentally changed or removed any files in error, then I suppose you could combine your `git add .` and `git commit` into `git commit -a -m "message"` (slightly different, will not track previously untracked files). – Steve Mar 31 '17 at 10:47
  • I think perhaps your real question here is "what is this index thing I keep hearing / reading about", or "what do I need to know about commits vs the index vs the work-tree". – torek Mar 31 '17 at 20:16

1 Answers1

0

git-rm - Remove files from the working tree and from the index

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. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

https://git-scm.com/docs/git-rm

Community
  • 1
  • 1
Ajay Singh
  • 1,251
  • 14
  • 17