Remove and ignore all files that have an extension from a git repository

22,380

Solution 1

Plenty of ways to remove them:

git ls-files | grep '\.pwc$' | xargs git rm

find . -name *.pwc | xargs git rm

Note: If you haven't committed them, just use rm, not git rm.

To ignore them in the future, simply add *.pwc to the .gitignore. (If you don't have one, create a file named .gitignore at the top level of your repository, and just add a single line saying "*.pwc")

Solution 2

You can also use the following:

git rm -r '*.pwc' 

and then make those files ignored by git:

echo '*.pwc' >> .gitignore

The last one is in case if you already have .gitignore file, if not, us single '>' sign.

Solution 3

In Windows this worked for me:

git rm -r '*.pwc' -f

And for keeping it in .gitignore

echo '*.pwc' >> .gitignore

Solution 4

Jefromi's answer will remove them for the present and the future...you could also remove them in the past using git filter-branch. Of course this has some other ramifications, like requiring everyone else working on the repo to re-checkout (and possibly rebase any work they haven't pushed to the main repo). Depends how big the PWC files are, you may want to do this if they are wasting a lot of diskspace in your repo (since every time you clone a git repo, you get every file and every revision)

Share:
22,380
BenMills
Author by

BenMills

Updated on July 09, 2022

Comments

  • BenMills
    BenMills almost 2 years

    I'm working on a django project with a few other developers and we have recently realized that all the .pwc files in our app cause the commits and repository to be cluttered.

    Is there any way I can remove all .pwc files from all child directories in my git repository and then ignore them for any future commit?

  • BenMills
    BenMills about 14 years
    I did later set up a .gitignore but will *.pwc keep them ignored for every directory level of the app?
  • Cascabel
    Cascabel about 14 years
    The .gitignore is respected in all subdirectories of the one where it's found. This is why they're generally placed at the top level. Note that you can put them at lower levels, if you'd like to have ignore rules only for a given subdirectory.
  • jiehanzheng
    jiehanzheng over 10 years
    True, but this won't look at subdirectories.
  • kaveish
    kaveish about 7 years
    @jiehanzheng That is not correct, the -r flag will also remove files in subdirectories.
  • Serhii Popov
    Serhii Popov over 4 years
    If you get an error find: paths must precede expression: 'filename.pwc', then change you command to find . -name '*.pwc' | xargs rm. Take a note on a single quotes.
  • Roly Poly
    Roly Poly over 3 years
    For me it was: git rm -r *.pdf, no quotes around the file name
  • Admin
    Admin over 3 years
    You are a lifesaver. You saved me a ton of time!