Search code examples
gitrm

Why use git rm instead of rm'ing files, directories, etc. and then using git add -u?


I'm having trouble getting git rm to delete a now superfluous directory and it's files from a project. This made me wonder why git rm is the accepted practice when deleting tracked files instead of rm -rf the directory and files outside of git and then git add -u to stage and prepare deletion of the previously tracked files? The latter seems to make much more sense to me but I probably dont understand the advantages of git rm.


Solution

  • Of course, there are always multiple ways to do something. Some advantages that using git rm has, are:

    • You actually control the index, not the real file system. The fact that Git removes the file in the file system as well is just a utility.
    • This means that you can remove files from Git without deleting them: git rm --cached.
    • You can precisely remove only those files you really want to be removed, unlike git add -u which adds all changes to the index.

    Especially the last one is very important to me personally, as I want full control about what change I add to a commit. So using add -u will rarely make me happy (just like add .). But of course, if you are happy with using add -u, feel free to use it.