Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore the permissions of files and directories within git if they have been modified?

I have a git checkout. All the file permissions are different than what git thinks they should be therefore they all show up as modified.

Without touching the content of the files (just want to modify the permissions) how do I set all the files permissions to what git thinks they should be?

like image 339
Dale Forester Avatar asked Mar 25 '10 16:03

Dale Forester


People also ask

How do I fix permissions in git?

Set the Appropriate Permissions on Objects Directory Make sure all your users who need access to git are part of the git group. Change the “git” in the above chgrp command to whatever group where all your developers belong to. The “s” option in the “g+rws” is to set the setuid bit on the objects folder.

How do I restore a directory in git?

STRONG SUGGESTION: 1) Do a "pull" from your remote repo into a NEW repo (don't do any more damage to your local repo). 2) Try "checkout" ... or even "revert" in your new, local, repo: atlassian.com/git/tutorials/undoing-changes/git-revert. 3) Update the remote repo when you're sure everything is OK.


2 Answers

Git keeps track of filepermission and exposes permission changes when creating patches using git diff -p. So all we need is:

  1. create a reverse patch
  2. include only the permission changes
  3. 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.

like image 170
muhqu Avatar answered Sep 24 '22 12:09

muhqu


Try git config core.fileMode false

From the git config man page:

core.fileMode

If false, the executable bit differences between the index and the working copy are ignored; useful on broken filesystems like FAT. See git-update-index(1).

The default is true, except git-clone(1) or git-init(1) will probe and set core.fileMode false if appropriate when the repository is created.

like image 32
Tim Henigan Avatar answered Sep 24 '22 12:09

Tim Henigan