Retaining file permissions with Git
The git-cache-meta
mentioned in SO question "git - how to recover the file permissions git thinks the file should be?" (and the git FAQ) is the more staightforward approach.
The idea is to store in a .git_cache_meta
file the permissions of the files and directories.
It is a separate file not versioned directly in the Git repo.
That is why the usage for it is:
$ git bundle create mybundle.bdl master; git-cache-meta --store
$ scp mybundle.bdl .git_cache_meta machine2:
#then on machine2:
$ git init; git pull mybundle.bdl master; git-cache-meta --apply
So you:
- bundle your repo and save the associated file permissions.
- copy those two files on the remote server
- restore the repo there, and apply the permission
How to restore the permissions of files and directories within git if they have been modified?
Git keeps track of filepermission and exposes permission changes when creating patches using git diff -p
. So all we need is:
- create a reverse patch
- include only the permission changes
- apply the patch to our working copy
As a one-liner:
git diff -p -R --no-ext-diff --no-color \
| grep -E "^(diff|(old|new) mode)" --color=never \
| git apply
you can also add it as an alias to your git config...
git config --global --add alias.permission-reset '!git diff -p -R --no-ext-diff --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply'
...and you can invoke it via:
git permission-reset
Note, if you shell is bash
, make sure to use '
instead of "
quotes around the !git
, otherwise it gets substituted with the last git
command you ran.
Thx to @Mixologic for pointing out that by simply using -R
on git diff
, the cumbersome sed
command is no longer required.
Can git retain file owner, permissions, ACLs, and file attributes on a local Windows NTFS machine?
GIT is a platform-independent code management tool, and can be run on numerous different operating systems. As a result, it is necessarily indifferent to any particular platform's notions of security or access control information. Security metadata about a file in Windows would be meaningless in, for example, a Linux environment, and vice-versa.
Related Topics
How to Tar a Directory Without Retaining the Directory Structure
How to Keep Environment Variables When Using Sudo
Can't Call C Standard Library Function on 64-Bit Linux from Assembly (Yasm) Code
What Is Rss and Vsz in Linux Memory Management
Static Link of Shared Library Function in Gcc
Syntax Error in Shell Script With Process Substitution
Get Program Execution Time in the Shell
Linux and I/O Completion Ports
How to Redirect Stderr and Stdout to Different Files in the Same Line in Script
Change the X-Frame-Options to Allow All Domains
Is There a Way For Non-Root Processes to Bind to "Privileged" Ports on Linux
How to Change Permissions For a Folder and Its Subfolders/Files
Understanding Linux /Proc/Pid/Maps or /Proc/Self/Maps
"Unable to Find Remote Helper For 'Https'" During Git Clone
How to Replace a String in Multiple Files in Linux Command Line