
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git rm
The git rm command is used to remove specific files from the index and working tree. By doing this, we remove the files from our project and stage their removal for the next commit.
The git rm command removes files from the Git index and, optionally, the working directory.
To remove a file only from the working directory (without affecting the index), use a different command like /bin/rm.
By default, git rm requires files to match the branch's latest commit, use the -f option to force removal of files that don't match.
Use git rm --cached to remove files only from the index, keeping them in the working directory.
With sparse-checkouts enabled, git rm only affects files within the sparse-checkout patterns.
Syntax
git rm <file>
The git rm is used to remove files that are specified by <file>.
Use the directory name with the -r option to delete all files from the directory and all of its subdirectories.
While git rm 'd*' only targets files within a given directory, file globbing matches across directory borders, allowing git rm 'd*' remove files in several directories.
Options
The git rm command has the following options:
-f
--force
git rm -f <file>
When using git rm, the -f or --force option overrides the default check that verifies files are up-to-date before removal.
-n
--dry-run
git rm -n <file>
With git rm, the -n or --dry-run option simulates the removal procedure without actually removing any files.
You can examine the changes before carrying out the actual removal because it displays which files would be removed from the index.
-r
--recursive
git rm -r <directory-name>
When a leading directory name is specified, the -r option in git rm allows us to perform recursive removal, which removes a directory together with all of its contents.
If this option is not present, git rm will just remove files and not go down into subdirectories.
--
git rm -- <file>
In Git, command-line parameters can be separated from the list of files or arguments using the -- option.
It is especially helpful in situations where file names could be misinterpreted as command-line options because it makes sure that any further arguments are handled as file paths rather than options.
--cached
git rm --cached <file>
When using git rm, the --cached option can be used to unstage files and remove them solely from the index, leaving the files in the working tree unaltered.
Regardless of whether they are altered or not, the files will be deleted from the staging area but will stay in our local directory.
--ignore-unmatch
git rm --ignore-unmatch <file>
When using the git rm command with the --ignore-unmatch option, the command will terminate with a success status of zero even if no files match the provided patterns.
When no files that match the requirements are found, this is helpful in preventing errors in scripts or tasks.
--sparse
git rm --sparse <file>
For pathways outside of the sparse-checkout cone, modifications to index entries are possible using the --sparse option in git rm.
With --sparse, git rm will update the index even for files that are not included in the sparse-checkout configuration, whereas normally it will limit changes to files inside the sparse-checkout cone.
-q
--quiet
git rm -q <file>
When using git rm, the -q or --quiet option suppresses the standard output listing each file being removed.
When this option is selected, the program removes the files without displaying the specifics of each file.
As a result, the terminal output becomes less cluttered.
You must commit the changes to finalize the file removal from the repository history, after the git rm command has been used.
Also, you need to be highly cautious while using the git rm -f command, when the file has uncommitted changes, as after using this command, those changes will be lost.