Today I Learned

A Hashrocket project

How to remove newly git ignored files

While developing a Cordova app I realized I was checking in files that are generated at build time. For example, the www folder is copied into each platform (ios, android). I was not sure what I could safely ignore until I looked at ember-cli-cordova’s gitignore.

So I changed my .gitignore to look something like:

...
# android
platforms/android/assets/www
platforms/android/bin
platforms/android/gen
platforms/android/local.properties
platforms/android/ant-build
platforms/android/ant-gen
platforms/android/CordovaLib/ant-build
platforms/android/CordovaLib/ant-gen
platforms/android/CordovaLib/bin
platforms/android/CordovaLib/gen
platforms/android/CordovaLib/local.properties
platforms/android/.gradle
platforms/android/res/xml/config.xml
...

But these folders are already checked in so now I have to git rm each of them.

Note: Don’t forget to use the --cached or you will actually delete the files

$ git rm -r --cached platforms/android/assets/www
$ git rm -r --cached platforms/android/bin
... and so on

This is super tedious and I could easily miss a file or directory. There has to be a better way!

$ git rm -r --cached . && git add .

Git already knows how to ignore the files in .gitignore so just untrack all files in the project and add them back.

The staged changes should now be the new lines in .gitignore and the deleted files that accompany the changes.