A file can be removed easily from Git index and working directory by Git rm command. For example:
After adding a file in Git, you may be required to remove 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:
If you decided to remove the file at this stage then the above rm command won’t do that. That is, running the:
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:
[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 a simple rm command will remove it from the index as well as the file system i.e. from the repo directory:
$ git rm tst1.txt rm 'tst1.txt'
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 the online repo while still needing to work on a code file that has been staged for uploading.
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 to remove a folder. Any files contained in the folder are also removed.
To see it in action, I have created a directory with the name of code in our demo repository:
Committing the changes:
After that, the push command is used for uploading changes to the online repo:
You can see that the online repo shows the code folder and a file inside it i.e. footer.php.
Deleting the directory:
This should result in:
rm 'code/footer.php'
Committing the changes after removing the directory:
Again running the push command:
If you refresh the online repo, the folder should have been removed there as well.
Keeping the folder in the file system example
The above command removes the folder/files from the Git index and working tree as well as from the file system.
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 the commit command, if you run the push command, you will see the folder no longer exists in the online repo.
Deleting multiple files example
Suppose, we have a folder named “del-demo” that contains five text files (tst1.txt, tst2.txt tst3.txt tst4.txt tst5.txt). We want to remove three files with a single command and upload the changes. This is how it can be done:
The output should be:
rm ‘del-demo/tst2.txt’
rm ‘del-demo/tst3.txt’
rm ‘del-demo/tst4.txt’
Now run the commit command:
The result:
[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.