我如何使 Git 忽略文件模式(chmod)更改?
我如何使 Git 忽略文件模式(chmod)更改?
我有一个项目需要在开发过程中使用 chmod
更改文件的模式为777,但是不应该在主要版本库中进行更改。
Git会检测到 chmod -R 777.
并标记所有文件为已更改。是否有一种方法可以使Git忽略对文件进行的模式更改?
admin 更改状态以发布 2023年5月25日
撤消工作树中的模式更改:
git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -d'\n' chmod +x git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -d'\n' chmod -x
或在mingw-git中
git diff --summary | grep 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -e'\n' chmod +x git diff --summary | grep 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -e'\n' chmod -x
或在BSD/macOS中
git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x
尝试:
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 repo或仅为一个git repo设置此标志:
# this will set your the flag for your user for all git repos (modifies `$HOME/.gitconfig`) # WARNING: this will be override by local config, fileMode value is automatically selected with latest version of git. # This mean that if git detect your current filesystem is compatible it will set local core.fileMode to true when you clone or init a repository. # Tool like cygwin emulation will be detected as compatible and so your local setting WILL BE SET to true no matter what you set in global setting. 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
和git init
在repo配置中显式设置core.fileMode
为true
,如在在克隆时本地覆盖全局core.fileMode false中讨论的那样
警告
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
。