Wednesday, 18 September, 2019 UTC


Summary

As your project changes over time, at some point you'll likely need to remove a file, or an entire directory, from the repository. Since this involves more than changing the contents of a file, Git has a special command to handle removing files, which also takes some important flags depending on your use-case.
In this short article we'll take a look at the git rm command and how to use it to remove files and directories from your repository.

Removing a File

The simplest usage of this command is to remove a single file from the repository, which you can do so using the following syntax:
$ git rm <filename>
Let's say we have a Git repo with the following contents:
$ git ls-files
.gitignore
index.js
lib/api.js
lib/db.js
package.json
For whatever reason, suppose we no longer need the lib/db.js file. In order to remove it from our repo, we'd run the following:
$ git rm lib/db.js 
rm 'lib/db.js'
Now, listing our repo files again shows that it was removed:
$ git ls-files
.gitignore
index.js
lib/api.js
package.json
However, we're not quite done. If you run the status command, you'll notice that the removal of the file is still in staging and needs to be committed to the repo, just like any other change you make.
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    lib/db.js
$ git commit -m "Removed db.js file"
[master cf191e4] Removed db.js file
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 lib/db.js

Removing Multiple Files

Removing multiple files from a repo is very similar to the previous use-case of removing a single file. Depending on your preferred method, there are a few ways to do this. For example, you can simply explicitly list all files to be removed:
$ git rm lib/db.js lib/api.js
rm 'lib/api.js'
rm 'lib/db.js'
Or, if there are more files than you feel like listing, you can always use the wildcard character:
$ git rm lib/*.js
rm 'lib/api.js'
rm 'lib/db.js'
Note that by removing all of the contents from the lib directory, Git will also remove the actual directory itself. This is done because Git only tracks files, not directories:
$ git ls-files
.gitignore
index.js
package.json

Removing a Directory

In order to remove an entire directory from your Git repository, all you need to do is remove all files in that directory from the repo. This can be done using one of methods above if there aren't many files, but in a lot of cases this would become too cumbersome. In this case, the best option would be to recursively remove all files using the -r flag.
$ git rm -r <directory>
For this example, we'll work off of our example above, and adding some more files to our lib directory:
$ git ls-files
.gitignore
index.js
lib/apis/aws.js
lib/apis/github.js
lib/apis/stripe.js
lib/db.js
package.json
Here we have nested files and directories within lib. To remove the entire lib directory from the repo altogether, we can use the -r flag:
$ git rm -r lib
rm 'lib/apis/aws.js'
rm 'lib/apis/github.js'
rm 'lib/apis/stripe.js'
rm 'lib/db.js'
Notice that this handles the removal of all files under lib, and even lib/apis, since it recursively searches the directory.
And finally, don't forget to commit your changes:
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    lib/apis/aws.js
    deleted:    lib/apis/github.js
    deleted:    lib/apis/stripe.js
    deleted:    lib/db.js

$ git commit -am "Removed lib directory"
[master 53fc6af] Removed lib directory
 4 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 lib/apis/aws.js
 delete mode 100644 lib/apis/github.js
 delete mode 100644 lib/apis/stripe.js
 delete mode 100644 lib/db.js