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

时间:2009-10-16 21:43:38

标签: git ignore chmod

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

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

11 个答案:

答案 0 :(得分:3435)

尝试:

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

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

git config --global 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,除非在非常罕见的环境中。

答案 1 :(得分:259)

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

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

答案 2 :(得分:123)

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

git config --global core.filemode false

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

git config --add --global core.filemode false

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

error: could not lock config file .git/config: No such file or directory

答案 3 :(得分:72)

如果

git config --global core.filemode false

不适合您,请手动执行:

cd into yourLovelyProject folder

cd到.git文件夹:

cd .git

编辑配置文件:

nano config

将true更改为false

[core]
        repositoryformatversion = 0
        filemode = true

- >

[core]
        repositoryformatversion = 0
        filemode = false

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

cd ..

重新启动git

git init

你完成了!

答案 4 :(得分:50)

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

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

答案 5 :(得分:34)

您可以全局配置:

git config --global core.filemode false

如果上述内容对您不起作用,原因可能是您的本地配置会覆盖全局配置。

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

git config --unset core.filemode

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

git config core.filemode false

答案 6 :(得分:20)

如果您已使用 chmod 命令,则检查文件的差异,它显示以前的文件模式和当前文件模式,例如:

新模式:755

旧模式:644

使用以下命令

设置所有文件的旧模式

<强> sudo chmod 644 .

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

git config core.fileMode false

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

sudo chmod 755 .

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

git config core.fileMode true

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

答案 7 :(得分:14)

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

答案 8 :(得分:14)

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

nfm = "!f(){ git -c core.fileMode=false $@; };f"

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

git nfm status

答案 9 :(得分:2)

简单解决方案:

在项目文件夹中点击简单推荐它不会删除您的原始更改) ...它只会删除更改在您更改项目文件夹权限 时,已完成

推荐如下:

git config core.fileMode false

为什么要修改所有不必要的文件: 因为您已更改项目文件夹权限 赞扬 sudo chmod -R 777 ./yourProjectFolder

你什么时候检查你做了什么改变? 您在使用 git diff filename

时发现如下所示

enter image description here

答案 10 :(得分:2)

这对我有用:

find . -type f -exec chmod a-x {} \;

或反向,具体取决于您的操作系统

find . -type f -exec chmod a+x {} \;