Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created August 11, 2015 21:02
Show Gist options
  • Select an option

  • Save jeffjohnson9046/80bc182db7ae2f4a6150 to your computer and use it in GitHub Desktop.

Select an option

Save jeffjohnson9046/80bc182db7ae2f4a6150 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
@prasket

prasket commented Apr 11, 2016

Copy link
Copy Markdown

Hey Jeff thanks for making this exactly what I was looking for!

@noplanman

Copy link
Copy Markdown

Perfect! Thanks so much for this.
Forgot the .gitignore too and this saved me from redoing a whole bunch of commits.

As you say:

(like an idiot)

@zacpwhite

Copy link
Copy Markdown

Thanks Jeff! This definitely saved me some time!

@shrukul

shrukul commented Oct 30, 2017

Copy link
Copy Markdown

Brilliant! Thanks Jeff :)

@shubhamsc

Copy link
Copy Markdown

awesome job
it's exactly same what i'm looking for
thank jeff

@sayinserdar

Copy link
Copy Markdown

Thanks a lot :) I've been searching for this, even github docs couldn't help me 👍

@DongYuYu

Copy link
Copy Markdown

Thanks, it's really helpful!

@hqn-scl

hqn-scl commented Feb 29, 2020

Copy link
Copy Markdown

Beautiful and perfect. Forked it for reference.

@VPavliashvili

Copy link
Copy Markdown

Thank you so much, this is what I was looking for

@ErinGe

ErinGe commented Mar 29, 2021

Copy link
Copy Markdown

Thanks a lot, really helpful!!!

@MightyOwler

Copy link
Copy Markdown

This is genius! Thank you!

@GlavS

GlavS commented Nov 15, 2022

Copy link
Copy Markdown

I would give 100 stars if I could... >_<
Thank you!!!

@ktMcD

ktMcD commented Dec 15, 2022

Copy link
Copy Markdown

You posted this more than six years ago and people are still benefitting from your efforts. Thank you so very much. If you were my neighbor I'd bake you something.

@OstenTV

OstenTV commented Mar 8, 2024

Copy link
Copy Markdown

For Windows, if anyone needs it :)

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

@cartesian-plane

Copy link
Copy Markdown

This can also be done via the web interface.
You have to open the file, then click the ... at the top right for the delete option.

@antoniopaolini

antoniopaolini commented Aug 3, 2024

Copy link
Copy Markdown

Great! Thanks a lot!
And thanks to @OstenTV too for the Win version.

@andikatp

Copy link
Copy Markdown

Thanks

@Shafkathasan

Shafkathasan commented May 14, 2025

Copy link
Copy Markdown
  • git rm --cached -r .
  • git add .
  • git commit -m "Remove ignored files from tracking"
  • git push origin master

@SpeedRanger

Copy link
Copy Markdown

Thanks @jeffjohnson9046. And also to @OstenTV for the Windows version

@nouxinf

nouxinf commented Jan 2, 2026

Copy link
Copy Markdown

For Windows, if anyone needs it :)

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

For the second command a powershell verion would be:

git ls-files -ci --exclude-standard |
ForEach-Object { git rm --cached $_ }

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