4) Undoing Changes in Git Lesson

Git Remove

13 min to complete · By CodingNomads Team

The git rm command removes tracked files from your staging area and directory. rm stands for remove, and the command is also called Git remove file. As the name suggests, the git rm command can be considered the opposite of the git add command.

In this lesson, learn to use git rm.

How Does Git Remove File Work?

To use the Git remove file command, add the filename(s) that you want to remove using the following syntax:

$ git rm <file Name>

The git rm command can remove one file, or it can remove many files at once. Running the Git remove file command deletes the files both from your staging area as well as from the directory, also known as the index and working tree, respectively defined below.

Colorful illustration of a light bulb

The git rm command operates only on the active branch of your repository. Other branches of your project will not be affected by it.

Index / Staging Area

The index, also called the staging area, is where your commits are prepared. The index compares the current files in your working directory with the files in your Git repository. When you make a change in the directory, the index marks the file as modified before you commit the change.

Working Tree / Directory

The working tree, also referred to as the directory or file system, consists of files that you are currently working on. You can think of it as a file system where you can view and also modify files.

Colorful illustration of a light bulb

You may see these terms used interchangeably in different resources.

By default, you cannot delete a file only from the working tree, but leave it in the staging area. During a git rm action, if Git finds that the HEAD of the staging area and the working tree are different, it will stop the process as a safety mechanism.

Later in this lesson, you'll learn how to remove a file only from the staging area.

Git Remove: Single File

Below are the steps to remove a single file using the git rm command:

  1. Create a new file and call it Test1.txt
  2. Add this file with git add, and commit it in your repo
  3. Run the git rm command:
$ git rm Test1.txt

As mentioned before, this command will delete the file both from your Git repository as well as from the filesystem. If you check the status of your repository, you see that Git is ready for you to commit the removal of this file into its permanent memory:

Git Remove a file

You can see that while the file is gone and Git has recorded the deletion in the staging area, the change has not yet been committed to Git's permanent history.

If you want to commit the deletion to your Git repo, you need to run git commit -m "your message".

Git Remove: Multiple Files

You can delete multiple files at once when you write the Git remove file command as follows:

$ git rm <file name1> <file name2> <file name3>

You can also use wildcard patterns to delete files with rm. For example, the following command deletes all .txt files in your repo:

$ git rm *.txt

Git RM --Cached: Remove Files From the Staging Area

Sometimes, you want to delete files from your index / staging area, but not from the directory / working tree. For example, when you accidentally commit a file that you don't want Git to keep track of, such as secrets and password files.

This happens more than you'd like to think, so here's how to remedy this type of situation.

If you want to keep files in your local repository but remove them from Git's staging area, you can add the --cached flag to the git rm command:

git rm --cached <file name>

Here's how to remove a file from the staging area only:

  1. Create another new file, calling it Test2.txt
  2. Add and commit the file to your repo
  3. Run the git rm --cached <File Name> command:
$ git rm --cached Test2.txt

After running the command, the file has been deleted from your Git history. Let's verify by running the git status command:

Git Remove Cached from Staging Area

This output means that it has been removed from the staging area, but Git can also still see the file in your working directory. That's why it also shows up as an untracked file. You can put it back at any time and resume tracking by calling git add on the file.

As you can see, the git rm --cached option is quite useful and something that will come in handy during your development work.

Git Remove Options

Let's look at some other options for the git rm command.

Git RM -N: Test Run

The -n option doesn't delete the file or files specified in the command; it only outputs information regarding which file the command would delete. It's a test run option that you can use to check first as a safety mechanism:

$ git rm -n <file name>

Git RM -R: Recursive Mode

The git rm -r option allows you to run in recursive mode. This means that Git will remove all contents of the target directory whose name you specified:

$ git rm -r <file name>

Git RM -F: Forced Deletion

The git rm -f option is used to force a deletion. It overrides Git's check to see the difference between your Git index and the file system. The deletion will go ahead even when there are uncommitted differences:

$ git rm -f <file name>

These Git remove options can be quite powerful, so handle them with care! You can always use the -n flag to make sure that your deletion command will actually do what you want it to do before running the command for real.

But what happens if you delete something that you didn't want to delete?

How to Undo Git RM

Thanks to version control, you can revert any changes made to your repository, even something that seems as final as the git rm command.

You can undo the git rm command with the git reset command. This is the go-to command for reverting any changes, which you will learn more about in an upcoming lesson. For now, here's how you undo the git rm command:

$ git reset HEAD
# or
$ git reset --hard

Both of these commands will revert the current index and the working tree to the HEAD commit of your Git repo. Effectively, if your most recent command was a git rm, and you haven't committed yet, this will reset the command's effects.

Another way to undo uncommitted changes is by running the git checkout command:

$ git checkout

Executing git checkout before committing changes you've made has the same effect as the git reset HEAD and git reset --hard commands mentioned above. It will restore your repo to the latest state that was saved to version control. You will learn more about git checkout in a later lesson.

Summary: Git Remove File

  • The git rm command removes tracked files from your staging area and the directory. rm stands for remove.
  • The git rm command can remove one file individually or multiple files at a time.
  • By default, the Git remove file command deletes the files both from your index / staging area and from the directory / working tree. You cannot delete a file from the working tree and leave it in the index.
  • The git rm --cached command allows you to remove files from the index / staging area but keep them in the directory / working tree.
  • Thanks to version control, you can undo git rm and revert any changes made to your repository with the git reset command.

For more information on git rm, check out the Git Rm documentation from Git.