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

问题描述 投票: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
投票

答案贴在这里:
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
© www.soinside.com 2019 - 2024. All rights reserved.