如何让 Git 忽略文件模式 (chmod) 更改? [英] How do I make Git ignore file mode (chmod) changes?
问题描述
我有一个项目,我必须在开发时将 chmod
的文件模式更改为 777,但不应在主存储库中更改.
Git 接收 chmod -R 777 .
并将所有文件标记为已更改.有没有办法让 Git 忽略对文件所做的模式更改?
尝试:
git config core.fileMode false
来自 git-config(1):
<块引用>core.fileMode告诉 Git 工作树中文件的可执行位是值得尊敬的.某些文件系统会丢失可执行位标记为可执行文件被检出,或检出一个具有可执行位的不可执行文件.git-clone(1)或 git-init(1) 探测文件系统以查看它是否处理可执行位正确并且这个变量是自动的根据需要设置.但是,存储库可能位于处理文件模式正确,并且当这个变量设置为 true 时创建,但以后可以从另一个访问丢失文件模式的环境(例如导出 ext4通过 CIFS 挂载,使用 Git 访问 Cygwin 创建的存储库适用于 Windows 或 Eclipse).在这种情况下,可能需要将此变量设置为false.请参阅 git-update-index(1).默认为 true(未指定 core.filemode 时在配置文件中).
-c
标志可用于为一次性命令设置此选项:
git -c core.fileMode=false diff
并且 --global
标志将使其成为登录用户的默认行为.
git config --global core.fileMode false
全局设置的更改不会应用于现有存储库.此外,git clone
和 git init
将 core.fileMode
在 repo 配置中显式设置为 true
,如Git global core.fileMode false 在克隆本地覆盖
警告
core.fileMode
不是最佳实践,应谨慎使用.此设置仅涵盖模式的可执行位,而不涵盖读/写位.在许多情况下,您认为需要此设置是因为您执行了诸如 chmod -R 777
之类的操作,使所有文件都可以执行.但在大多数项目中出于安全原因,大多数文件不需要也不应该是可执行的.
解决这种情况的正确方法是分别处理文件夹和文件权限,例如:
find .-type d -exec chmod a+rwx {} ;# 使文件夹可遍历和读/写找 .-type f -exec chmod a+rw {} ;# 使文件读/写
如果你这样做,你将永远不需要使用 core.fileMode
,除非在非常罕见的环境中.
I have a project in which I have to change the mode of files with chmod
to 777 while developing, but which should not change in the main repo.
Git picks up on chmod -R 777 .
and marks all files as changed. Is there a way to make Git ignore mode changes that have been made to files?
Try:
git config core.fileMode false
From 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).
The -c
flag can be used to set this option for one-off commands:
git -c core.fileMode=false diff
And the --global
flag will make it be the default behavior for the logged in user.
git config --global core.fileMode false
Changes of the global setting won't be applied to existing repositories.
Additionally, git clone
and git init
explicitly set core.fileMode
to true
in the repo config as discussed in Git global core.fileMode false overridden locally on clone
Warning
core.fileMode
is not the best practice and should be used carefully. This setting only covers the executable bit of mode and never the read/write bits. In many cases you think you need this setting because you did something like chmod -R 777
, making all your files executable. But in most projects most files don't need and should not be executable for security reasons.
The proper way to solve this kind of situation is to handle folder and file permission separately, with something like:
find . -type d -exec chmod a+rwx {} ; # Make folders traversable and read/write
find . -type f -exec chmod a+rw {} ; # Make files read/write
If you do that, you'll never need to use core.fileMode
, except in very rare environment.
这篇关于如何让 Git 忽略文件模式 (chmod) 更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!