Skip to content

[Edit] Git: Remove #6608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 64 additions & 28 deletions content/git/concepts/rm/rm.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,111 @@
---
Title: 'Remove'
Description: 'A command for removing files or directories from a Git repository.'
Description: 'Removes files or directories from a Git repository.'
Subjects:
- 'Bash/Shell'
- 'Developer Tools'
Tags:
- 'Command Line'
- 'Files'
- 'Git'
- 'Version Control'
CatalogContent:
- 'learn-the-command-line'
- 'learn-git'
---

The **`git rm`** command is used for removing files or directories from a Git repository.
The **`git rm`** command is used for removing files or directories from a Git repository. This command ensures that the deletion of files is tracked in the version history, making it a reliable way to manage file removals in a Git repository.

## Deleting Files
Common applications of `git rm` include deleting unnecessary files, cleaning up large directories, or untracking files that should no longer be version-controlled (such as log files or temporary data). It helps in maintaining a clear and consistent [commit](https://www.codecademy.com/resources/docs/git/commit) history reflecting the file removals.

To remove a file from Git, it has to be removed from tracked files (from the staging area) and then committed. The `git rm` command accomplishes this task. It also removes the file from the working directory so that it's no longer an untracked file.
## Syntax

### Deleting a File From the Working Directory
```pseudo
git rm [<options>] <file>...
```

If a file is deleted from the working directory, it will appear in the "Changes not staged for commit" section of the `git status` output.
In the syntax:

```shell
rm PROJECTS.md
git status
```
- `<file>...`: The file(s) to remove from the Git repository.
- `[<options>]`: The options to be used with the `git rm` command. Common ones include:
- `-f`, `--force`: Used to forcefully remove a file. It needs to be used with caution as it permanently removes the file from the working directory.
- `--cached`: Used to remove a file from the repository but retain it in the working directory. It effectively stops tracking the file, but the file itself remains in the local filesystem.
- `-r`: Used to remove a directory and its contents recursively.
- `--dry-run`: Shows what would be removed without actually removing anything.

### Utilizing `git rm` for Staged File Removal
## Example 1: Using `git rm` to Remove a File

Running `git rm` stages the removal of the file, preparing it for the next commit.
To remove a file from the Git repository, it has to be removed from tracked files (from the staging area) and then committed. The `git rm` command accomplishes the first task. It also removes the file from the working directory so that it's no longer an untracked file.

This example uses `git rm` to remove a file named `PROJECTS.md` from the repository:

```shell
git rm PROJECTS.md
git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: PROJECTS.md
```

### Options
Running `git rm` stages the removal of the file, preparing it for the next commit.

The `git rm` commands supports several options, including:
## Example 2: Using `git rm` to Remove a Folder

- `-f`, `--force`: Used to forcefully remove a file. Use with caution as it permanently deletes the file from the working directory.
The `git rm` command can also be utilized to remove a folder and its contents from the Git repository.

- `--cached`: Used to remove a file from the Git repository but retain it in the working directory. It effectively stops tracking the file, but the file itself remains in the local filesystem.
This example uses `git rm` with the `-r` option to remove a folder named `myfolder` from the repository:

- `-r`: Used to remove a directory and its contents recursively.
```shell
git rm -r myfolder
```

- `--dry-run`: Demonstrates what would be removed without actually removing anything.
This command is commonly used for cleaning up large directories or removing modules.

### Examples
## Example 3: Using `git rm` to Untrack a File

#### Remove a files
The `git rm` command can be used for untracking a file in the Git repository as well. Untracking a file indicates that changes to the file will not be tracked anymore, but it will be kept in the working directory.

To remove a file name `example.txt` from the repository.
This example uses `git rm` with the `--cached` option to untrack a file named `important.doc` in the repository:

```shell
git rm example.txt
git rm --cached important.doc
```

#### Remove a Directory and its Contents
This command is often used when sensitive or machine-specific files are accidentally committed and need to be excluded using [`.gitignore`](https://www.codecademy.com/article/how-to-use-gitignore).

## Frequently Asked Questions

### 1. How to undo `git rm`?

To remove a directory named `myfolder` and all its contents.
If you haven’t committed the removal yet, you can recover the deleted file using:

```shell
git rm -r myfolder
git restore <file>
```

#### Remove a File but Keep it in Working Directory
If you have committed the removal, you can recover the deleted file using:

```shell
git checkout HEAD <file>
```

### 2. What’s the difference between `git rm` and just deleting the file manually?

Manually deleting the file removes it from the working directory, but Git won’t know about the deletion until you run [`git add`](https://www.codecademy.com/resources/docs/git/add) on the deleted file or use `git rm`. In contrast, `git rm` stages the deletion immediately, making it part of the next commit automatically.

### 3. How can I remove multiple files at once using `git rm`?

To stop tracking a file `important.doc` but keep it in the working directory.
You can specify all the files manually:

```shell
git rm --cached important.doc
git rm <file1> <file2> <file3>,,,
```

Or, use a wildcard:

```shell
git rm *.log
```

This command deletes all `.log` files from the Git repository and stages the deletions for the next commit.