如何让Git忽略文件模式(chmod)的变化?

Mar*_*tin 2188 git ignore chmod

我有一个项目,我必须chmod在开发过程中将文件模式更改为777,但不应在主回购中更改.

Git选择chmod -R 777 .并将所有文件标记为已更改.有没有办法让Git忽略对文件进行的模式更改?

Gre*_*ill 3626

尝试:

git config core.fileMode false
Run Code Online (Sandbox Code Playgroud)

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).
Run Code Online (Sandbox Code Playgroud)

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

git -c core.fileMode=false diff
Run Code Online (Sandbox Code Playgroud)

--global标志将使其成为登录用户的默认行为.

git config --global core.fileMode false
Run Code Online (Sandbox Code Playgroud)

警告

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

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

find . -type d -exec chmod a+rwx {} \; # Make folders traversable and read/write
find . -type f -exec chmod a+rw {} \;  # Make files read/write
Run Code Online (Sandbox Code Playgroud)

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

  • 如果你执行`git config --global core.filemode false`,你只需要为所有repos做一次. (189认同)
  • 这对我来说不起作用,直到我修复了它应该是fileMode而不是filemode (12认同)
  • @donquixote:`git config`命令将设置写入正确的配置文件(仅针对当前存储库的`.git/config`,或者如果与`--global`一起使用,则为`〜/ .gitconfig`). (10认同)
  • @tishma:Git配置部分和变量名称[根据文档不区分大小写,请参阅CONFIGURATION FILE部分](https://www.kernel.org/pub/software/scm/git/docs/git-config.html ),所以如果上述内容对您不起作用,则原因不同. (7认同)
  • @ zx1986:没关系.从[git config](https://git-scm.com/docs/git-config):"变量名称不区分大小写,......" (7认同)
  • 为了使文件/目录权限能够在OSX和ubuntu上工作(我得到了一个`find:unknown predicate'-t'`错误),我不得不将其更改为:`find.-type d -exec chmod 755 {} \;`和`find.-type f -exec chmod 644 {} \;` (6认同)
  • 请为此答案添加第二个警告,说明全局设置不会应用于现有的回购!对于每个repo,你需要运行本地命令(或者似乎"git init"具有相同的效果).这将影响到每个人,并且可能非常令人困惑(特别是当您第一次与第二个回购进行交互时,并且不知道为什么全局设置在第一个回购工作时无效,在那里您运行全局和本地设置的版本更改.) (6认同)
  • @GregHewgill 当然,我只是想强调,与大多数其他设置(例如“autocrlf”)不同,git 二进制文件将为克隆上的每个存储库在本地设置“core.filemode”:[git-clone 或 git-init 探测文件系统查看它是否正确处理可执行位,并且该变量会根据需要自动设置。](https://git-scm.com/docs/git-config#git-config-corefileMode) (5认同)
  • @greg甚至在进行全局编辑后,如果本地配置已经有filemode = True,它将无法工作.本地值将覆盖全局设置...我必须转到我的机器中的每个repo并将每个本地存储库的filemode的本地配置值更改为false (4认同)
  • 我不知道行为是否发生了变化,但现在在克隆时的每个存储库 git 配置中显式设置了“core.fileMode”。因此,顶部评论中建议的“git config --global core.filemode false”将无效,因为它被本地设置覆盖。 (4认同)
  • --global 仅对新克隆的存储库生效。 (3认同)
  • 请注意,“git init”和“git clone”将忽略“--global”设置并显式将“core.filemode”设置为“true”。请参阅 /sf/ask/2127462291/ 了解解决方法。 (3认同)
  • 这是我第50次来这里记住这个命令。 (3认同)
  • 如果您遇到必须更改文件权限并从 Windows 计算机提交 git 的情况:http://blog.lesc.se/2011/11/how-to-change-file-premissions-in-git .html (2认同)
  • @GregHewgill:这会忽略提交时的模式更改吗?从 Windows 保存时,samba 似乎将文件设置为 +x。如果我无论如何都提交的话,将 fileMode 设置为 false 会执行更新索引吗?或者它只会提交文本更改而不提交 chmod 更改?谢谢! (2认同)
  • 如果它不工作,试试这个:`git filter-branch -f --tree-filter'find*-type f | xargs chmod 644' - --all` (2认同)
  • 我必须通过 `git init` 来使这个配置生效。 (2认同)
  • 从损坏的 git 存储库恢复后,我的文件具有错误的权限。最好实际重新设置它们,而不是让 git 忽略它们: find 。-d 型 | xargs chmod 0755 查找 . -类型 f | xargs chmod 0644 (http://stackoverflow.com/questions/14892289/is-this- Correct-to-re-set-up-permissions-for-rails-app) (2认同)
  • 到底是“filemode”还是“fileMode”? (2认同)
  • 描述已更改,现在可以更正确地反映问题 `core.fileMode ... 当签出标记为可执行的文件或签出带有可执行位的非可执行文件时,某些文件系统会丢失可执行位。git-clone[1] 或 git-init[1] 探测文件系统以查看它是否正确处理可执行位,并根据需要自动设置此变量。 (2认同)
  • 我可能已经回到这一行大约 9876 次,找到它比记住更容易:) (2认同)

yod*_*oda 268

在工作树中撤消模式更改:

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
Run Code Online (Sandbox Code Playgroud)

或者在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
Run Code Online (Sandbox Code Playgroud)

  • 在OS X Lion上,省略`xargs`中的`-d'\n'`部分,因为这是一个非法的参数(并且不需要). (42认同)
  • 很酷,'tr`的事情奏效了!这是OSX的完整命令:`git diff --summary | grep --color'模式改变100644 => 100755'| cut -d'' - f7- | tr'\n''\ 0'| xargs -0 chmod -x` (10认同)
  • 您可以忽略有关"chmod:在'+ x'之后缺少操作数"的任何错误 (8认同)
  • @Pascal @pimlottc -d指定分隔符为换行符而不是任何空格.BSD xargs没有那个选项,但你可以通过`tr'\n''\ 0'`管道输出,然后使用`-0` arg到xargs使用NUL作为分隔符. (7认同)
  • 这是最新的吗?我得到了"chmod:太少的争论" (5认同)
  • 对我来说,它给了 `chmod: '+x' 之后缺少操作数 (4认同)
  • grep --color有什么帮助? (2认同)

小智 131

如果要为所有存储库设置此选项,请使用该--global选项.

git config --global core.filemode false
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,您可能正在使用更新版本的git,请尝试使用该--add选项.

git config --add --global core.filemode false
Run Code Online (Sandbox Code Playgroud)

如果你在没有--global选项的情况下运行它并且你的工作目录不是repo,你就会得到

error: could not lock config file .git/config: No such file or directory
Run Code Online (Sandbox Code Playgroud)

  • 如果repo的本地配置已经有filemode = true,则更改全局配置将无济于事,因为本地配置将覆盖全局配置.将不得不改变一次机器的每个repo的本地配置 (14认同)
  • 看起来后来GIT使用`--add`,如`git config --add --global core.filemode false` (5认同)
  • 请:用syedrakib的警告更新这个答案!在我找到它之前,一切都感到疯狂,之后感觉很完美. (3认同)

Sin*_*dem 79

如果

git config --global core.filemode false
Run Code Online (Sandbox Code Playgroud)

不起作用,手动完成:

cd into yourLovelyProject folder
Run Code Online (Sandbox Code Playgroud)

cd进.git文件夹:

cd .git
Run Code Online (Sandbox Code Playgroud)

编辑配置文件:

nano config
Run Code Online (Sandbox Code Playgroud)

将true更改为false

[core]
        repositoryformatversion = 0
        filemode = true
Run Code Online (Sandbox Code Playgroud)

- >

[core]
        repositoryformatversion = 0
        filemode = false
Run Code Online (Sandbox Code Playgroud)

保存,退出,转到上层文件夹:

cd ..
Run Code Online (Sandbox Code Playgroud)

重新启动git

git init
Run Code Online (Sandbox Code Playgroud)

你完成了!

  • 而不是编辑`.git/config`,项目根目录中的简单`git config core.fileMode false`就足够了.如果您编辑配置文件,则最好完全删除该指令,以便拾取全局指令. (10认同)
  • -1如果git config --global不起作用,则表示你没有在系统级别执行此操作的权限,删除`global`选项与手动编辑.git/config完全相同 (5认同)
  • `git init` 将文件模式恢复为 TRUE! (3认同)
  • 一旦运行了 `git init` 我们应该将 filemode 设置回 true 吗? (2认同)

Jak*_*ski 51

添加到Greg Hewgill的答案(使用core.fileMode配置变量):

您可以使用git update-index--chmod=(-|+)x选项(低级版本的"git add")来更改索引中的执行权限,如果您使用"git commit"(而不是"git commit",则可以从中获取权限) ").

  • @Greg:一个人需要有足够的积分来编辑自己的答案; 我想我当时没有足够的编辑权限. (6认同)
  • 这应该被编辑成Greg Hewgill的答案,而不是作为一个单独的答案添加,从而创建一个具有单一明确表示的最高答案. (2认同)

Tyl*_*ong 37

您可以全局配置它:

git config --global core.filemode false

如果上述方法不适合您,原因可能是您的本地配置会覆盖全局配置.

删除本地配置以使全局配置生效:

git config --unset core.filemode

或者,您可以将本地配置更改为正确的值:

git config core.filemode false

  • 如果主要答案对你没有帮助 - 试试这个.如果你想检查本地配置而不修改它,请检查`git config -l`(列出当前配置 - 本地和全局) (3认同)

小智 21

如果你已经使用过chmod命令,那么检查文件的差异,它显示以前的文件模式和当前文件模式,例如:

新模式:755

旧模式:644

使用以下命令设置所有文件的旧模式

sudo chmod 644 .

现在使用命令或手动将配置文件中的core.fileMode设置为false.

git config core.fileMode false
Run Code Online (Sandbox Code Playgroud)

然后应用chmod命令来更改所有文件的权限,例如

sudo chmod 755 .
Run Code Online (Sandbox Code Playgroud)

并再次将core.fileMode设置为true.

git config core.fileMode true
Run Code Online (Sandbox Code Playgroud)

对于最佳实践,请不要始终将core.fileMode保留为false.


Vil*_*lle 17

通过定义以下别名(在〜/ .gitconfig中),您可以轻松地临时禁用fileMode per git命令:

[alias]
nfm = "!f(){ git -c core.fileMode=false $@; };f"
Run Code Online (Sandbox Code Playgroud)

当此别名以git命令作为前缀时,文件模式更改将不会显示将显示它们的命令.例如:

git nfm status
Run Code Online (Sandbox Code Playgroud)


dry*_*obs 13

如果要在递归的配置文件中将filemode设置为false(包括子模块): find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'

  • 如果该行不在配置文件中,则无效.如果你想为子模块改变它,试试这个:`git submodule foreach git config core.fileMode false` (4认同)

Sha*_*pta 5

简单的解决方案:

打这个简单的命令在项目文件夹(它不会删除你原来的变化) ......它只会移除变更已被做了,而你更改项目文件夹的权限

命令如下:

git 配置 core.fileMode 假

为什么所有不必要的文件都被修改: 因为您已经使用sudo chmod -R 777 ./yourProjectFolder更改了项目文件夹权限

你什么时候会检查你没有做的变化?您在使用git diff 文件名时发现如下所示

old mode 100644
new mode 100755
Run Code Online (Sandbox Code Playgroud)