Home > Net >  How can I recursively remove all json files with Git?
How can I recursively remove all json files with Git?

Time:03-25

My project contains hundred of directories and subdirectories and I would like to remove all JSON files from git but keep them in the project

I tried

git rm -r ./*.json

but it's only removed the JSON files from the current directory. Is there a way to recursively remove them from all directories?

CodePudding user response:

Have fun:

find . -name "*json" | xargs git rm

I assume you've backed up the json files.

  • The find command will find all the files ending in json, it includes the ones in subdirectories.
  • The pipe | is a way to send those results the the next command.
  • To paraphrase wikipedia: xargs converts input from standard input (the pipe) into arguments to a command, which is git rm in this case.

CodePudding user response:

Git itself supports recursive globbing via the pathspec.

git rm ':(glob)./**/*.json'

In this case, it's git, not the shell, that's expanding the pattern. git rm './**/*.json' would look for a literal file named ./**/*.json to remove; the :(glob) enables pattern matching.

For more information, see the entry for "pathspec" in man gitglossary.

If you want to remove the files from the repository only (but not the files in the current working directory), add the --cached option to the command:

git rm --cached ':(glob)./**/*.json'
  •  Tags:  
  • git
  • Related