Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Revert file permission changes in local git repository.. Very useful when you change permissions due to working on a samba share. Lifted from: http://stackoverflow.com/questions/2517339/git-how-to-recover-the-file-permissions-git-thinks-the-file-should-be
git diff -p \
| grep -E '^(diff|old mode|new mode)' \
| sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' \
| git apply
@Dzenly

This comment has been minimized.

Copy link

Dzenly commented Mar 8, 2018

Just in case -p option is not about 'permissions', it is about 'patch'. Also this option is used by default, so can be omited.
https://git-scm.com/docs/git-diff

Also in pointed topic, there is an improvement:

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

This comment has been minimized.

Copy link

hadaytullah commented Jun 17, 2019

Thanks! appreciate the help.

@abilogos

This comment has been minimized.

Copy link

abilogos commented Oct 13, 2019

thanks. very helpful for me.
it just applies (revert) only file mode changes (diff|(old|new) mode).
Thanks a lot.

@RaphiStein

This comment has been minimized.

Copy link

RaphiStein commented Dec 31, 2019

Any ideas on how to filter out files that have changes in their content?
For example, I want all the files with permissions changes but not if they also have changes in their content

@bene20

This comment has been minimized.

Copy link

bene20 commented Mar 16, 2020

Excelent!
If you have configured an external diff tool, i recommend you to add the --no-ext-diff parameter to avoid it to open that tool a billion times:

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

@mariusvw

This comment has been minimized.

Copy link

mariusvw commented Jul 3, 2020

Seems to fail on deleted files, this resolve that

Command

git diff -p --no-ext-diff --no-color --diff-filter=d | grep -E "^(diff|old mode|new mode)" | sed -e "s/^old/NEW/;s/^new/old/;s/^NEW/new/" | git apply

Git Alias

git config --global --add alias.permission-resetb '!git diff -p --no-ext-diff --no-color --diff-filter=d | grep -E "^(diff|old mode|new mode)" | sed -e "s/^old/NEW/;s/^new/old/;s/^NEW/new/" | git apply'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.