A file can be removed easily from Git index and working directory by Git rm command. For example:
git rm some_file
After adding a file in Git, you may require removing it for different reasons and at various levels. You may remove it from the index, working tree as well as file system or just remove it from the index and working tree.
The next section takes you through different scenarios of removing files and directories in different states.
Scenario 1: The example of removing a file after a commit
In this example, I will delete a file after it is committed or staged for uploading/pushing to the remote repository.
Suppose, we added a file (tst1.txt) in our master branch in the local repo as follows:
$ git add tst1.txt
If you decided to remove the file at this stage then the above rm command won’t do that. That is, running the:
$ git rm tst1.txt
will produce the following error:
error: the following file has changes staged in the index: tst1.txt (use --cached to keep the file, or -f to force removal)
As we run the commit command:
$ git commit -m “Text file added”
[master 8547f8c] Text file added 1 file changed, 1 insertion(+) create mode 100644 tst1.txt
if you decide to remove the file after a commit is made then simple rm command will remove it from the index as well as file system i.e. from the repo directory:
$ git rm tst1.txt rm 'tst1.txt'
After running this command, if you look at the folder, the tst1.txt file should have been removed there as well.
What if I want to keep the file in the directory?
You may want to remove the file from the working tree or index only while keeping it in the file system. For example, you want to commit other changes in online repo while still need to work on a code file that has been staged for uploading.
In that case, you may run the rm command with –cached flag.
Suppose, we again have a tst1.txt file that has been added and committed and now we want to remove it from the working tree:
$ git rm --cached tst1.txt
If you check the folder, you will see the tst1.txt file is still there. After this command, just run the commit command and push the changes to the remote repository.
Removing a directory example
You may use the –r option in the Git rm command for removing a folder. Any files contained in the folder are also removed.
The –r option in rm command allows recursive removal if you provide a leading directory name.
To see it in action, I have created a directory with the name of code in our demo repository:
$ git add code
Committing the changes:
$ git commit -m “Folder added”
After that, the push command is used for uploading changes to the online repo:
$ git push origin master
You can see, the online repo shows the code folder and a file inside it i.e. footer.php.
Deleting the directory:
$ git rm -r code
This should result in:
Committing the changes after removing the directory:
$ git commit -m “Code Folder removed”
Again running the push command:
$ git push origin master
If you refresh the online repo, the folder should have been removed there as well.
Keeping the folder in file system example
The above command removes the folder/files from the Git index and working tree as well as from the file system as well.
If you want to keep the folder in the file system and just remove it from the working tree then use the –cached option as follows. Consider, we have a folder code2 containing two files. This is added and committed in the local and remote repos. For deleting it from the repo:
$ git rm -r --cached code2
If you look at the folder, the directory (code2) should be in place.
After commit command, if you run the push command, you will see the folder is no more exist in the online repo.
Deleting multiple files example
Suppose, we have a folder name “del-demo” that contains five text files (tst1.txt, tst2.txt tst3.txt tst4.txt tst5.txt). We want to remove three files by the single command and upload the changes. This is how it can be done:
$ git rm del-demo/tst2.txt del-demo/tst3.txt del-demo/tst4.txt
The output should be:
Now run the commit command:
$ git commit -m “Removed three text files”
[master 2a90231] Removed three text files 3 files changed, 3 deletions(-) delete mode 100644 del-demo/tst2.txt delete mode 100644 del-demo/tst3.txt delete mode 100644 del-demo/tst4.txt
Now you are good to go for making a push and making changes online.