AlgoMaster Logo

Removing Files (git rm)

Last Updated: January 3, 2026

5 min read

Removing files from a Git repository can feel daunting, but with the right understanding of git rm, this task becomes straightforward.

Let's dive into how this command works, its options, and practical scenarios where it's beneficial.

What is git rm?

To begin tracking files, you use the git add command. This command informs Git that you want to start tracking changes to specific files.

The git rm command is used to remove files from both your working directory and the index (staging area). This means that not only does it delete the file from your local filesystem, but it also stages this deletion for your next commit.

Unlike simply deleting files with your operating system's file management system, using git rm ensures that Git tracks the removal and can reflect these changes in your commit history.

How It Works Under the Hood

When you execute git rm, Git performs two key actions:

  1. Removal from the Working Directory: The specified file is deleted from your local filesystem.
  2. Staging the Removal: The deletion is staged, which prepares it for the next commit. This change is reflected in the index.

To visualize this, consider the following representation of Git's structure before and after running git rm:

Basic Usage of git rm

The simplest way to remove a file is by using the command followed by the file name:

This command does two things: it removes file.txt from your working directory and stages this change. It’s essential to ensure that you really want to delete the file, as this action cannot be undone without recovery methods.

Confirming the Removal

After running git rm, you can confirm that the file has been staged for removal by using:

You should see a message indicating that the file has been deleted and is ready to be committed:

Common Options for git rm

git rm comes with several options that can modify its behavior, allowing for greater flexibility in file management.

Recursive Removal

If you want to remove a directory along with all its contents, use the -r option:

This will delete the directory and all files contained within it. Be cautious when using this option, as it permanently removes the directory structure.

Force Removal

In cases where a file has been modified and is not staged, Git will prevent its removal to avoid data loss. If you're sure you want to remove such a file, use the -f option:

This command forces the removal of modified_file.txt, regardless of its state in the working directory.

Keep the Local Copy

Sometimes, you may want to remove a file from the Git index but keep it in your working directory. In this case, you can use the --cached option:

This command removes file.txt from the staging area but leaves it in your working directory. It's useful when you realize a file should not be tracked by Git anymore.

Scenarios for Using git rm

Understanding when to use git rm can enhance your workflow and reduce clutter in your repository. Here are a few common scenarios:

Cleaning Up Unused Files

As a project evolves, certain files may become unnecessary, such as temporary files, outdated scripts, or configuration files. Using git rm helps you remove these files systematically:

By regularly cleaning up your repository, you maintain a more manageable project structure.

Resolving Merge Conflicts

During a merge, you may encounter conflicts that require the removal of certain files. If a file is no longer necessary in the merged state, you can use git rm to resolve this conflict:

After resolving conflicts and staging the removal, you can proceed with the merge commit.

Managing Large Files

If a large file was mistakenly added to your repository, you might want to remove it completely. Using:

will allow you to untrack the file while keeping it in your working directory, preventing bloating of your repository size.

Recovery Methods After Using git rm

Mistakes happen, and sometimes you may accidentally remove a file you didn’t intend to. Don’t worry; there are ways to recover.

Recovering a Deleted File

If you’ve accidentally removed a file and haven’t committed the change yet, you can restore it using:

This command restores file.txt from the last committed state.

Recovering After a Commit

If you’ve already committed the removal, you can still recover the file by checking out the previous commit where the file existed:

This command retrieves the file from the commit prior to the last one, effectively restoring it back to your working directory.

Now that you understand how to use git rm to cleanly remove files from your repository, you are ready to explore git mv.

In the next chapter, we will look at how to rename and move files within your project while keeping Git tracking them seamlessly.