Last active
November 9, 2024 11:45
-
-
Save jtdp/5443498 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git diff -p \ | |
| grep -E '^(diff|old mode|new mode)' \ | |
| sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' \ | |
| git apply |
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'
Not working(
What if it was commited and pushed? I've one commit with more than 3000 file permissions changed (mixed with real content changes). What I can do to revert the file permissions?
I was able to restore permissions from a previous commit doing this:
git ls-tree -r d3fb093 |while read mode _ _ fpath; do chmod "${mode:3}" "$fpath"; done
where d3fb093
is the last commit with the right permissions.
tahnks! ;)
Awesome!
If this solution does not work for you, I have found two possible causes -
- Your git clone is on an ExFAT formatted volume. There is no fix for this, and no commands will help. ExFAT doesn't support file permissions. You can only configure git to ignore file permissions, or reformat the drive using something more suitable.
- Your git clone is on a volume shared via Apple File Sharing and has an ACL at the root of the volume. If that's you, good luck, and I have no idea, but maybe this tip will send you in the right direction!
It isn't a solution, in that it doesn't change or fix file permissions, but if you are stuck with one of the above, it may ease the pain to set:
git config core.fileMode false
though using this setting is highly not recommended generally.
@martynov-dm you should run git permission-resetb
not git apply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems to fail on deleted files, this resolve that
Command
Git Alias