git中的文件和目录被修改后如何恢复权限? [英] How to restore the permissions of files and directories within git if they have been modified?

查看:87
本文介绍了git中的文件和目录被修改后如何恢复权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 git checkout.所有文件权限都与 git 认为的不同,因此它们都显示为已修改.

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.

在不触及文件内容的情况下(只想修改权限)如何将所有文件权限设置为 git 认为它们应该是什么?

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?

推荐答案

当使用 git diff -p 创建补丁时,Git 会跟踪文件权限并公开权限更改.所以我们只需要:

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

  1. 创建一个反向补丁
  2. 仅包括权限更改
  3. 将补丁应用到我们的工作副本

作为单线:

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

您也可以将它作为别名添加到您的 git 配置中...

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

注意,如果你的 shell 是 bash,请确保在 !git' 而不是 " 引号code>,否则它会被您运行的最后一个 git 命令替换.

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.

感谢@Mixologic 指出通过在git diff 上简单地使用-R,不再需要繁琐的sed 命令.

Thx to @Mixologic for pointing out that by simply using -R on git diff, the cumbersome sed command is no longer required.

这篇关于git中的文件和目录被修改后如何恢复权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆