如果已修改git中文件和目录的权限,如何还原它们? [英] How to restore the permissions of files and directories within git if they have been modified?

查看:137
本文介绍了如果已修改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

注意,如果外壳为bash,请确保在!git周围使用'而不是"引号,否则它将替换为您运行的最后一个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天全站免登陆