While dealing with a large complex project, developers create multiple files. Sometimes, they manually delete unnecessary files from the disk. However, those deleted files are still tracked by Git and take up space in the repository. It can also cause conflict with other developers if you try to merge or push changes. So, it is important to remove the deleted files from the Git repository and make the repository clean.
This write-up will explain the methods to remove multiple files from the Git repository that have already been deleted from the disk.
How to Remove/Delete Multiple Files From a Git Repository That Have Already Been Deleted Manually From Disk?
Different Git commands are used to remove multiple files from a Git repository that have already been deleted from the Disk, such as:
Method 1: Remove Multiple Files From Git Repo Using the “git add -u” Command
To remove multiple files from the Git repository, first, navigate to the particular repository. Then, execute the “git add -u” command and commit changes. Check out the provided section for a practical demonstration.
Step 1: Move to Local Repository
First, write out the below-listed command and switch to a desired local directory:
Step 2: Check Git Status
Next, view the current status of the working repository:
According to the below-provided screenshot, the current repository contains multiple files that have been deleted manually from the disk:
Step 3: Track Files
Then, utilize the “git add” command along with the “-u” option to stage all files:
Here, the “-u” option is used for updating the index. This will make Git know that the deleted files are part of the next commit:
Step 4: Commit Changes
Now, commit the deleted files using the given-below command:
Step 5: Verify Changes
Lastly, ensure that the manually deleted files have been removed from the Git repository by viewing the Git status:
It can be observed that the files have been removed from the Git repository and Git status is clear now:
Method 2: Remove Multiple Files From Git Repo Using the “git ls-files –deleted -z | xargs -0 git rm” Command
Another way to remove multiple files from the Git repository is to utilize the “git ls-files –deleted -z | xargs -0 git rm” command. To do so, check out the below-listed steps.
Step 1: Check Git Status
First, view the current status of the directory using the below-provided command:
It can be observed that the list of manually deleted files has been displayed as output:
Step 2: Remove Files
Then, type out the following command to remove the mentioned files:
Step 3: Commit Changes
Now, commit deleted files to save changes:
Step 4: Verification
Finally, verify changes by viewing the Git status:
As you can see that the files have been removed from a Git repository:
We have explained methods to remove multiple files from the Git repository that have already been deleted from the disk.
Conclusion
To remove multiple files from a Git repository that have been deleted manually, first, navigate to the local Git repository. Then, execute the “git add -u” or “git ls-files –deleted -z | xargs -0 git rm” command. Next, commit the deleted files and verify the changes. This write-up explained the procedures to remove multiple files from the Git repository that have already been deleted from the disk.