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

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
  2. WordPress Transients Explained - A Developer's Guide to Site Performance
  3. Behind the Click - The Hidden Journey of Your Web Requests
  4. Automating Code Linting with GitHub Actions for WordPress Plugins
  5. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer

Your Questions / Comments

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