一尘不染

如何让 Git 忽略文件模式 (chmod) 更改?

git

我有一个项目,我必须chmod在开发时将文件模式更改为 777,但不应在主仓库中更改。

Git 接受chmod -R 777 .并将所有文件标记为已更改。有没有办法让 Git 忽略对文件所做的模式更改?


阅读 292

收藏
2022-01-30

共1个答案

一尘不染

尝试:

git config core.fileMode false

git-config(1)

```
core.fileMode
Tells Git if the executable bit of files in the working tree
is to be honored.

Some filesystems lose the executable bit when a file that is
marked as executable is checked out, or checks out a
non-executable file with executable bit on. git-clone(1)
or git-init(1) probe the filesystem to see if it handles the 
executable bit correctly and this variable is automatically
set as necessary.

A repository, however, may be on a filesystem that handles
the filemode correctly, and this variable is set to true when
created, but later may be made accessible from another
environment that loses the filemode (e.g. exporting ext4
via CIFS mount, visiting a Cygwin created repository with Git
for Windows or Eclipse). In such a case it may be necessary
to set this variable to false. See git-update-index(1).

The default is true (when core.filemode is not specified
in the config file).

```

-c标志可用于为一次性命令设置此选项:

git -c core.fileMode=false diff

键入-c core.fileMode=false可能很麻烦,因此您可以为所有 git 存储库或仅为一个 git 存储库设置此标志:

# this will set your the flag for your user for all git repos (modifies `$HOME/.gitconfig`)
git config --global core.fileMode false

# this will set the flag for one git repo (modifies `$current_git_repo/.git/config`)
git config core.fileMode false

此外,git clone并在 repo 配置中git init显式设置core.fileMode为Git global core.fileMode false 在克隆上本地覆盖true

警告

core.fileMode不是最佳做法,应谨慎使用。此设置仅涵盖模式的可执行位,而不涵盖读/写位。在许多情况下,您认为您需要此设置,因为您执行了类似chmod -R 777的操作,使您的所有文件都可执行。但在大多数项目中,出于安全原因,大多数文件不需要也不应该是可执行的

解决这种情况的正确方法是分别处理文件夹和文件权限,例如:

find . -type d -exec chmod a+rwx {} \; # Make folders traversable and read/write
find . -type f -exec chmod a+rw {} \;  # Make files read/write

如果你这样做,你将永远不需要使用core.fileMode,除非在非常罕见的环境中。

2022-01-30