How to Delete a File or a Directory from a Git Repository
Sometimes it is necessary to delete a particular file from your git repository. Learn how to do it following the guidelines presented in this tutorial.
Sometimes you need to delete a particular file or a directory from a git repository. In case you have accidentally committed a sensitive file to your <kbd class="highlighted">git repository</kbd> and now want to delete it, follow the guidelines below.
Steps to Deleting a File From Repository
Note that since Git is a version control system, it will allow you to recover the deleted files anytime you need it. Here are the 3 steps to follow:
Removing files or directories
To remove a file both from the <kbd class="highlighted">git repository</kbd> and the file system, run the git rm command as follows:
remove a file from the git repository and the file system
git rm file1.txtTo delete a particular file only from the git repository and leave it in the file system, you should use the --cached attribute in the following way:
delete the file only from the git repository
git rm --cached file1.txtIn case you want to delete multiple files at once, you should list all the file names as it is shown below:
How to Delete a File from a Git Repository
git rm file1.txt file2.txtYou can specify the file name by putting asterisk as a wildcard. It will delete all the files that match the pattern.
How to Delete a File from a Git Repository
git rm file*To delete a folder, run the <kbd class="highlighted">git rm</kbd> command in a recursive (<kbd class="highlighted">-r</kbd>) mode:
How to Delete a File from a Git Repository
git rm -r folder-nameCommitting the changes
The next step is committing the changes by running the git commit command:
How to Delete a File from a Git Repository
git commit -m "remove file1.txt"Pushing changes
Finally, you need to push the changes to your remote repository using the git push command like this (suppose, the name of remote is origin, which is by default) :
push the changes to the remote repository git
git push origin <branch-name>The git rm Command
Whenever it is necessary to delete a particular file or a group of files from a <kbd class="highlighted">git repository</kbd>, you can use the <kbd class="highlighted">git rm</kbd> command. Additionally, you can use this command to remove files from the staging index and the working directory.
Consider that this command gives no option for removing a file only from the working directory. In case an inconsistency occurs between the <kbd class="highlighted">HEAD</kbd> version of a file and the staging index or the working tree version, the removal will be blocked by Git.
The git push Command
This command is used for uploading the content of the local repository to the remote. It can be considered as the opposite of the git fetch command. The latter is targeted at importing files to the local branches.
The <kbd class="highlighted">git push</kbd> belongs to the commands that are included in the “syncing” process. Such commands work on remote branches that are organized by the git remote command. You can upload commits by running <kbd class="highlighted">git push</kbd> and download by invoking <kbd class="highlighted">git fetch</kbd> and git pull.