Sarathlal N

Remove ignored files / directory from Git repository

Adding a .gitignore file to our repo allows us to specify which files we don’t need to be tracked in our git repo. If we initially added the .gitignore file with the file & directory names to be ignored, git never track those files & directories. But if the files / directory are already in our git repo, the files / directory will continue there. Now we need to manually delete them from repo.

Remove files / directory by manually selecting files

git rm --cached file_1 file_2 dir_1

Remove files / directory from .gitignore file

git rm --cached `git ls-files -i -c --exclude-from=.gitignore`

The above command lists all files / directory to be excluded as per .gitignore file and deletes them.

If the above command does not work, try the following command instead.

git ls-files -i -c --exclude-from=.gitignore | xargs git rm --cached

This command creates separate rm commands for each file & folder mentioned in .gitignore file, using xargs command.

Now we have to commit the changes.

git add .
git commit -m "Drop files from .gitignore"
git push

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.