cd /news/developer-tools/remove-unwanted-files-from-a-git-rep… · home topics developer-tools article
[ARTICLE · art-12608] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Remove unwanted files from a git repo AFTER adding a .gitignore. The files will remain on disk.

This article provides a script to remove files from a Git repository that were committed before a `.gitignore` file was added. The commands use `git ls-files` to identify and un-track files matching the ignore rules, followed by a commit to finalize the removal. The files themselves remain on the local disk.

read1 min views25 publishedAug 11, 2015

git-ignore.sh

  This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters

#

#

#

#

See the unwanted files:

git ls-files -ci --exclude-standard

#

Remove the unwanted files:

git ls-files -ci --exclude-standard -z |

xargs -0 git rm --cached

#

Commit changes

git commit -am " Removed unwanted files marked in .gitignore "

#

Push

git push origin master

#

or whatever branch you're on

── more in #developer-tools 4 stories · sorted by recency
── more on @visual studio 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/remove-unwanted-file…] indexed:0 read:1min 2015-08-11 ·