Today I Learned

Giant Monkeys Learning

Fixing file permissions in git repository

If you mess up your file permissions in a git repository you can easily reset them back to how they were with this simple command:

git diff -p -R --no-color \
    | grep -E "^(diff|(old|new) mode)" --color=never  \
    | git apply

From this gist: https://gist.github.com/jtdp/5443498

This is not a usual problem, and can if on the off chance it does occure you can normally solve it by a simple checkout or hard reset, but in my case I also had uncommited changes to files in the repository alongside the permission changes which I really wanted/needed to keep!

Sidenote

If you are wondering how you could end up in this situation in the first place… well, my story is I backed up a complete copy of my projects folder onto a HD when I installed my laptop from fresh. This was the easiest way for me to ensure that I would have all of my local changes in projects just in case I had forgotten to commit and push somewhere! After copying them back every repo was full of file permission changes.