Skip to content

Instantly share code, notes, and snippets.

@OstenTV
Forked from jeffjohnson9046/git-ignore.sh
Created August 3, 2024 15:48
Show Gist options
  • Save OstenTV/047260254fdf061e366cb7eb90db520b to your computer and use it in GitHub Desktop.
Save OstenTV/047260254fdf061e366cb7eb90db520b to your computer and use it in GitHub Desktop.
Remove unwanted files from a git repo AFTER adding a .gitignore. The files will remain on disk.
## I just ran into this after initializing a Visual Studio project _before_ adding a .gitignore file (like an idiot).
## I felt real dumb commiting a bunch of files I didn't need to, so the commands below should do the trick. The first two commands
## came from the second answer on this post: http://stackoverflow.com/questions/7527982/applying-gitignore-to-committed-files
# 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
@OstenTV
Copy link
Author

OstenTV commented Aug 3, 2024

`git ls-files -ci --exclude-standard

REM Remove the unwanted files:
FOR /F "tokens=*" %G IN ('git ls-files -ci --exclude-standard') DO git rm --cached "%G"

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

REM Push
git push origin master`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment