git.pngGit

Git rm

In this article, we will learn How to use git rm command to remove the files from the git repository.

Total Views:   1516


In this article, we will learn How to use the "git rm" command to remove the files from the git repository. The “git rm” command removes the file from the git repository as well from the file system based upon the options passed in the command. The basic syntax of the command is as below:

“git rm <file_name>”

In order to demonstrate, I already have a git repository that already contains multiple tracked files as shown in the below image.

 

1. Remove file from Git repository as well as delete from the file system.

Use “git rm <file_name>” in order to delete the file from the git repository as well as from the file system. You can also remove multiple files by passing the filename delimited with space. On successful execution of the command, the file will be deleted from the system.

 

Use the “git commit” command with a meaningful message to push the changes to the local git repository.

 

How to use the git commit command in the Git Bash?

2. Remove or untrack a file from the Git repository, not from the file system.

Use the “git rm <file_name> --cached” command (with the –cached option) in order to remove the file from the git repository. The actual file is not deleted from the file system.

 

In the above image, I removed the index.html file from the git repository only. With “git status” you can check that the Index.html file is untracked.

3. Dry run

Use “git rm <file_name> --dry-run” to just check the files that would remove but actually no files with be deleted from the system. It is useful while you are deleting files recursively (by using the -r option) and want to check the files going to remove from the repository.

 

You can use the -h option in order to check the complete available option which can be used with the git rm command.