如何重置已提交的文件权限更改(模式更改)?

问题描述 投票:0回答:1

我更改了 git 存储库的所有文件和目录的权限,并将其提交到我的主分支,例如。

mode change 100755 => 100644 path/to/file.sh

我现在想重置这些更改并使用

git reset --hard HEAD~1
,但文件权限仍然相同。

我尝试在没有这些文件权限更改的情况下签出另一个分支,但文件权限仍然相同。

我尝试了在网上找到的片段

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

但出现此错误:

error: No valid patches in input (allow with "--allow-empty")

如何将这些“模式更改”文件权限更改重置为 HEAD~1 中的权限?

当我强制推送主分支的另一个未更新版本时,它会起作用吗?

git file-permissions git-reset
1个回答
0
投票

错误:输入中没有有效补丁(允许使用“--allow-empty”)

  • 您收到此错误是因为您对文件执行了
    git add
    ,这意味着没有进行任何更改。
  • 您找到的代码片段正在处理您尚未进行的
    commited
    更改。
  1. 查看下面的演示以查看其工作情况
  2. 第二个演示将重现您的错误。

答案贴在这里:
git内的文件和目录被修改后如何恢复权限?

按预期工作:

注意

  • 如您在演示中看到的,有一项更改
  • 当尝试重置某些文件时,获得了原始提交中不存在的
    w
    权限。
#!/bin/bash

# Clone the original repo
cd        /tmp
rm -rf    /tmp/local-repo
git init  /tmp/local-repo

# Switch to the new repo
cd /tmp/local-repo

# Set permissions to file
for i in {4..7};
do 
  echo $i > $i.txt 
  chmod $i$i$i $i.txt
done  

# Commit the changes
git add .
git commit -m "Initial commit"

# View current permissions
echo -e "----------------------------------------------------------------"
echo -e "Initial permissions"
echo -e ""
ls -la

# Now lets change the permissions of the files
chmod 777 *.txt

# View current permissions
echo -e ""
echo -e "----------------------------------------------------------------"
echo -e "Updated permissions"
echo -e ""
ls -la

# revert prevoius permissions
echo -e ""
echo -e "----------------------------------------------------------------"
echo -e "Reverted permissions"
echo -e ""

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

# View current permissions
ls -la

输出:

Initialized empty Git repository in /private/tmp/local-repo/.git/
[main (root-commit) d29864b] Initial commit
 4 files changed, 4 insertions(+)
 create mode 100644 4.txt
 create mode 100755 5.txt
 create mode 100644 6.txt
 create mode 100755 7.txt
----------------------------------------------------------------
Initial permissions

total 32
drwxr-xr-x   7 nirg  wheel  224 Apr 27 00:35 .
drwxrwxrwt  12 root  wheel  384 Apr 27 00:35 ..
drwxr-xr-x  14 nirg  wheel  448 Apr 27 00:35 .git
-r--r--r--   1 nirg  wheel    2 Apr 27 00:35 4.txt
-r-xr-xr-x   1 nirg  wheel    2 Apr 27 00:35 5.txt
-rw-rw-rw-   1 nirg  wheel    2 Apr 27 00:35 6.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 7.txt

----------------------------------------------------------------
Updated permissions

total 32
drwxr-xr-x   7 nirg  wheel  224 Apr 27 00:35 .
drwxrwxrwt  12 root  wheel  384 Apr 27 00:35 ..
drwxr-xr-x  14 nirg  wheel  448 Apr 27 00:35 .git
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 4.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 5.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 6.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 7.txt

----------------------------------------------------------------
Reverted permissions

total 32
drwxr-xr-x   7 nirg  wheel  224 Apr 27 00:35 .
drwxrwxrwt  12 root  wheel  384 Apr 27 00:35 ..
drwxr-xr-x  14 nirg  wheel  448 Apr 27 00:35 .git
-rw-r--r--   1 nirg  wheel    2 Apr 27 00:35 4.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 5.txt
-rw-r--r--   1 nirg  wheel    2 Apr 27 00:35 6.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 7.txt
# Original start
-r--r--r--   1 nirg  wheel    2 Apr 27 00:35 4.txt
-r-xr-xr-x   1 nirg  wheel    2 Apr 27 00:35 5.txt

# After "recover"
# Extra "w"
-rw-r--r--   1 nirg  wheel    2 Apr 27 00:35 4.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:35 5.txt

2.重现您的错误

  • 将文件设置为 000,001 等权限不允许将文件添加到 git,
    git add
    将失败,结果将是您得到的错误。
  • 由于没有文件添加到 git,这是当您尝试运行代码片段时出现的错误
error: No valid patches in input (allow with "--allow-empty")
## Set the permissions form 000-777
Initialized empty Git repository in /private/tmp/local-repo/.git/
error: open("0.txt"): Permission denied
error: unable to index file '0.txt'
fatal: adding files failed
On branch main

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        0.txt
        1.txt
        2.txt
        3.txt
        4.txt
        5.txt
        6.txt
        7.txt

nothing added to commit but untracked files present (use "git add" to track)
----------------------------------------------------------------
Initial permissions

total 64
drwxr-xr-x  11 nirg  wheel  352 Apr 27 00:32 .
drwxrwxrwt  12 root  wheel  384 Apr 27 00:32 ..
drwxr-xr-x  11 nirg  wheel  352 Apr 27 00:32 .git
----------   1 nirg  wheel    2 Apr 27 00:32 0.txt
---x--x--x   1 nirg  wheel    2 Apr 27 00:32 1.txt
--w--w--w-   1 nirg  wheel    2 Apr 27 00:32 2.txt
--wx-wx-wx   1 nirg  wheel    2 Apr 27 00:32 3.txt
-r--r--r--   1 nirg  wheel    2 Apr 27 00:32 4.txt
-r-xr-xr-x   1 nirg  wheel    2 Apr 27 00:32 5.txt
-rw-rw-rw-   1 nirg  wheel    2 Apr 27 00:32 6.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 7.txt

----------------------------------------------------------------
Updated permissions

total 64
drwxr-xr-x  11 nirg  wheel  352 Apr 27 00:32 .
drwxrwxrwt  12 root  wheel  384 Apr 27 00:32 ..
drwxr-xr-x  11 nirg  wheel  352 Apr 27 00:32 .git
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 0.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 1.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 2.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 3.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 4.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 5.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 6.txt
-rwxrwxrwx   1 nirg  wheel    2 Apr 27 00:32 7.txt

----------------------------------------------------------------
Reverted permissions

error: No valid patches in input (allow with "--allow-empty")
© www.soinside.com 2019 - 2024. All rights reserved.