0

I'm new to using git and was wondering how do I remove all cached .exe files from a git repository (I want to keep them in my working folder). The root folder has subfolders that also have the .exe files. Will the following command work or do I have to do something else? I understand the -r is for removing directories and not files and it just seems wrong, and on searching about this topic, only relevant thing I found was using find and delete commands, but I'm not sure about how to use them.

git rm --cached -r *.exe

Edit: I ran the command and it only removed the .exe from root folder not the sub folders Is manually listing all sub folder paths and running that command the only option?

Since passing the git command with it's cached flag is required, for people who are unfamiliar with chaining commands in linux/ unix, the recursive remove unix answer is not sufficient.

Xgh05t
  • 230
  • 2
  • 11
  • That command looks fine to me, [-r](https://git-scm.com/docs/git-rm#Documentation/git-rm.txt--r) is for recursiveness which is what you are after. The question in your title does not match the actual question though – Paolo Mar 02 '20 at 09:48
  • I did not make a `.gitignore` before committing and well only option at that point is removing those files from the cache, which effectively is gitignoring them after commit, but I understand how it might not seem related? And the command did not remove files from sub folders only root – Xgh05t Mar 02 '20 at 09:55
  • What shell are you using? Works for me – Paolo Mar 02 '20 at 10:02
  • @UnbearableLightness Git Bash (I'm on windows) – Xgh05t Mar 02 '20 at 10:03
  • Does this answer your question? [Recursively remove files](https://stackoverflow.com/questions/2016844/recursively-remove-files) – phd Mar 02 '20 at 11:35
  • https://stackoverflow.com/search?q=%5Bbash%5D+remove+files+recursively – phd Mar 02 '20 at 11:35
  • `shopt -s globstar && rm **/*.exe` – phd Mar 02 '20 at 11:35
  • @phd it answers parts of the question, but I don't want to remove files from my directory, only from the git repo, I'm also new to linux commands so where the git with rm --cached would go, I'm not sure I will learn with time though. and I have it mentioned in my body of the question as well – Xgh05t Mar 02 '20 at 17:47

2 Answers2

1

This should do it (in an sh compatible shell):

find . -name '*.exe' -exec git rm --cached '{}' \;

Alternatively, less elegant but easier to compose:

ls -R | grep '.exe$' | xargs git rm --cached

The trouble with your git rm -r command is that it will only recurse into directories whose name ends with .exe, which is obviously not what you want.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Years of experience, I'm afraid. It helps to know what tools are available and what each of them can do. At first you have to read their manpages to find out how to use them, but eventually you learn the most common options by heart. Then you just put the building blocks together. – Thomas Mar 02 '20 at 10:39
0

Escape the wildcard so Git sees it rather than your shell:

git rm -n --cached \*.exe

(then rerun without the -n if you like what you see)

jthill
  • 55,082
  • 5
  • 77
  • 137