How To Remove Files With git rm

Tim Mouskhelichvili
Tim Mouskhelichvili
3 minutes to read

Git is an excellent version control software that tracks file changes inside a code repository. It provides a lot of easy-to-use commands to manage your files. Sometimes, developers want to remove a file (or a collection of files) from the Git index. That's why the git rm command exists.

To remove a file from the Git staging index, use:

bashgit rm file.txt --cached

To remove a file from the Git staging index AND the filesystem, use:

bashgit rm file.txt

By the end of this article, you will learn everything about the git rm command, how it is different from a simple rm, how to remove a folder, and many more.

Let's get to it 😎.

git rm

The definition

The git rm command removes a tracked file or a collection of tracked files from the Git index.

Additionally, this command can:

To run this command, you need to open a terminal from your Git repository's location and type:

bashgit rm <file>

The <file> argument can be file paths, wildcard patterns, directory names, or a combination of them.

The git rm command supports many different options to alter its behavior.

OptionDescription
-f
--force
Overrides the Git safety check. This safety check ensures that the files in the HEAD match the staging index and the working directory current content.
-n
--dry-run
Git will NOT DELETE the files but will output the names of the affected files.
-rRemove a folder/directory AND its content recursively.
--cachedThe removal of files will only happen on the staging index and NOT on the working directory.
--ignore-unmatchIf no matched files are found, no error will be output, and the command will gracefully exit.
-q
--quiet
Hide the output of the command.

Here are other git commands to remove/undo changes:

How to remove a file?

To remove a file from the Git staging index but keep it in the working directory, use the git rm cached command.

bashgit rm --cached file.txt

To remove a file from the Git staging index AND remove it from the working directory, use the git rm command.

bashgit rm file.txt

You will need to commit and push to finalize the removal.

How to remove multiple files?

You can remove multiple files, like so:

bashgit rm file.txt file2.txt

Or select the files using a wildcard:

bashgit rm dir/*.txt

This command will remove all the text files inside the dir folder.

How to remove a folder?

To remove a folder from the git index, use the recursive option with the git rm command.

bashgit rm -r --cached directory

To remove a folder from the git index AND the working directory, type:

bashgit rm -r directory

How to remove untracked files?

To remove untracked files from a git repository, you need to use the git clean command.

You can read more about the git clean command in an extensive article that I wrote.

Git rm vs rm

git rm vs rm

When you remove a file with the rm command, it will be deleted from the filesystem but kept in the Git staging index.

Using git rm ensures that the tracked file is deleted from the Git staging index AND the working directory.

Final thoughts

It is very easy to remove tracked files from the Git staging index and the working directory by using the git rm command.

If you make a mistake and want to undo the action, go into your git reflog and use the git reset command with the previous commit's checksum.

Comments (0)
Reply to: