Git tip: resetting file permissions

I'd like to share a little trick I have been using a lot the last few months.

Have you ever been in the situation where you changed file permissions on some files within a certain git repository and where you'd like to reset the permissions, but you can't use git checkout because you don't want to lose actual changes to the file?

Here's a handy command I found online:

git diff -p \
    | grep -E '^(diff|old mode|new mode)' \
    | sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' \
    | git apply

This script lists all changed permissions with git diff -p (the -p option makes git diff only show permission changes) and then uses some clever UNIX piping to feed a rewritten git diff log to git apply.

You can even create an alias like 'permission-reset' with the following command

git config --global --add alias.permission-reset '!git diff -p | grep -E "^(diff|old mode|new mode)" | sed -e "s/^old/NEW/;s/^new/old/;s/^NEW/new/" | git apply'

After creating this alias you can use git permission-reset to execute the multiple piped commands in one simple, rememberable git commmand.

Looks interesting?

Are you looking for someone who can assist you with your web project?
Someone with know-how who can handle your project completely in-house?

Contact Me!