In this guide, I will show you how to remove files and directories using the git rm command along with an alternate method.
Below are important points to be noted before removing files or directories in git.
- When files or directories are removed you should create a commit to persist the changes.
- You can use the rm shell command to remove files or directories. Alternatively, you can also use the git rm command.
- Git will block the file removal if you try to remove uncommitted changes to an existing tracked file.
- By mistake, if you deleted a wrong file you can easily recover it using the git checkout or git reset command.
SAMPLE PROJECT
If you wish to follow along, you can run the following commands and create a sample project. I am using Linux OS for demonstration purposes. The following commands can be submitted in any Linux OS, Git Bash, MacOS, or WSL. The only requirement is git should be installed on your machine.
STEP 1 – Create a project directory and initialize an empty git repository.
$ mkdir -p /tmp/git-rm-tmp/rmdir1
$ cd /tmp/git-rm-tmp
$ git init .
Initialized empty Git repository in /tmp/git-rm-tmp/.git/
STEP 2 – Create two commits.
# COMMIT 1 #
$ echo "sample commit 1 for git-rm demo" > rmdir1/git-rm-file1.txt
$ git add .
$ git commit -m "sample commit - git-rm-file1.txt"
[main (root-commit) 1c7b316] sample commit - git-rm-file1.txt
1 file changed, 1 insertion(+)
create mode 100644 rmdir1/git-rm-file1.txt
# COMMIT 2 #
$ echo "sample commit 2 for git-rm demo" > rmdir1/git-rm-file2.txt
$ git add .
$ git commit -m "sample commit - git-rm-file2.txt"
[main 3fb196c] sample commit - git-rm-file2.txt
1 file changed, 1 insertion(+)
create mode 100644 rmdir1/git-rm-file2.txt
Now two files are created under the rmdir1 directory. You can run the git log command to check the log information.
# Detailed log #
$ git log
# Oneliner #
$ git log --oneline
3fb196c (HEAD -> main) sample commit - git-rm-file2.txt
1c7b316 sample commit - git-rm-file1.txt
METHOD 1 – Three-Step Process To Remove Files & Directories In Git
In this method to remove files are directories you have to follow the below flow.
- Remove the file/directory.
- Add the change to the staging index.
- Create a new commit.
STEP 1 – Remove The File/Directory
We will remove git-rm-file1.txt which we created in the previous section. In case of multiple files, all you have to do is pass the file name as arguments to the rm command. You can also use the glob expression to remove files.
# SYNTAX #
$ rm <file>
$ rm <file1> <file2> <fileN>
$ rm <*.txt>
# RM COMMAND #
$ rm git-rm-file1.txt
STEP 2 – Add The Change To Staging Index
Run the git status command to check the status of the deleted file. The file will be marked as “deleted”.
$ git status
On branch main
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: git-rm-file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
You have to add the change to the staging index before creating a commit.
$ git add .
or
$ git add git-rm-file1.txt
STEP 3 – Create A New Commit
Create a new commit by running the following command. You can modify the commit message as per your wish.
$ git commit -m "DEL :: Removed git-rm-file1.txt"
[main e0b2336] DEL :: Removed git-rm-file1.txt
1 file changed, 1 deletion(-)
delete mode 100644 rmdir1/git-rm-file1.txt
METHOD 2 – TWo Step Process To Remove Files & Directories In Git
In this method to remove files or directories you have to follow the below flow.
- Run the git rm command to remove files/directories which will automatically remove the files and add the change to staging.
- Create A new commit.
STEP 1 – Run Git RM Command
The git rm command will remove the file/directory and automatically add the changes to the staging index. You can pass single or multiple file names as the argument. The git rm command also supports globbing.
# SYNTAX #
$ git rm <file1>
$ git rm <file1> <file2> <fileN>
$ git rm file*.txt
$ git rm DIR/\*.txt
# COMMAND #
$ git rm rmdir1/git-rm-file2.txt
rm 'rmdir1/git-rm-file2.txt'
Run the git status command and you will see the changes are staged and ready to be committed.
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: rmdir1/git-rm-file2.txt
STEP 2 – Create A New Commit
A new commit can be created by running the following command.
$ git commit -m "DEL :: Removed git-rm-file2.txt"
[main 638e70f] DEL :: Removed git-rm-file2.txt
1 file changed, 1 deletion(-)
delete mode 100644 rmdir1/git-rm-file2.txt
Different Flags Supported By Git RM Command
1. Force Remove Using -f Flag
You can use the rm command to remove files that are already committed. If you made any changes to the file and tried removing it without committing you will get an error.
$ echo "adding another line to git-rm-file1.txt" >> git-rm-file1.txt
$ git rm git-rm-file1.txt
error: the following file has local modifications:
rmdir1/git-rm-file1.txt
(use --cached to keep the file, or -f to force removal)
You can use the -f flag to force remove the file and create a new commit.
$ git rm -f git-rm-file1.txt
rm 'rmdir1/git-rm-file1.txt'
2. Remove Files Only From Staging Index Using --cached
If you wish to keep the file in the working directory but remove the existing file from the staging index use the --cached flag. If you see the output of the git status command, the file copy in the staging index is deleted and the file in the working directory is now in untracked status.
$ git rm --cached rmdir1/git-rm-file1.txt
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: rmdir1/git-rm-file1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
rmdir1/git-rm-file1.txt
3. Dry Run Using -n Or –dry-run Flag
You can do a dry run before actually deleting the files by either using the -n or --dry-run flag.
$ git rm -n rmdir1/git-rm-file1.txt
rm 'rmdir1/git-rm-file1.txt'
4. Recursive Removal Using -r Flag
The -r flag will recursively remove the files and directories along with the given directory.
$ git rm -r rmdir1
rm 'rmdir1/git-rm-file1.txt'
rm 'rmdir1/git-rm-file2.txt'
$ ls -l /tmp/git-rm-tmp
total 0
5. Quiet output using -q or --quiet Flag
The -q or --quiet flag will suppress the output for the git rm command.
$ git rm -r -q rmdir1
Summary Of Commands
COMMAND | USAGE |
---|---|
rm | Removes single file |
rm | Removes multiple files |
rm *.txt | Removes all files that end with .txt. File globbing |
rm -rf DIR | Removes directory recursively |
git rm | Remove the file and stage the changes |
git rm | Remove file and stage the changes |
git rm -r DIR | Removes multiple files and stages the changes |
git rm -f | Force remove the file and stage the changes |
git rm –cached | Removes the file from staging alone |
git rm -n | Dry run |
git rm -q | Suppress the output message |
Wrap Up
In this article, we have seen two different methods to remove files and directories that are tracked by Git. The git rm method automatically stages the changes and also comes with different flags. If any wrong files are removed, you have the option to recover using the git checkout or reset command.