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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1580596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:13:49  来源:igfitidea点击:

How do I make Git ignore file mode (chmod) changes?

gitignorechmod

提问by Marcus Westin

I have a project in which I have to change the mode of files with chmodto 777 while developing, but which should not change in the main repo.

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

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?

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

回答by Greg Hewgill

Try:

尝试:

git config core.fileMode false

From git-config(1):

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).
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 -cflag can be used to set this option for one-off commands:

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

git -c core.fileMode=false diff

And the --globalflag will make it be the default behavior for the logged in user.

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

git config --global core.fileMode false

Changes of the global setting won't be applied to existing repositories. Additionally, git cloneand git initexplicitly set core.fileModeto truein the repo config as discussed in Git global core.fileMode false overridden locally on clone

全局设置的更改不会应用于现有存储库。此外,git clone并按照Git global core.fileMode false 中讨论的在 repo 配置中git init显式设置core.fileModetrue克隆本地覆盖

Warning

警告

core.fileModeis 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.

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

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.

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

回答by yoda

undo mode change in working tree:

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

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

Or in mingw-git

或者在 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

回答by adrien

If you want to set this option for all of your repos, use the --globaloption.

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

git config --global core.filemode false

If this does not work you are probably using a newer version of git so try the --addoption.

如果这不起作用,您可能使用的是较新版本的 git,因此请尝试该--add选项。

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

If you run it without the --global option and your working directory is not a repo, you'll get

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

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

回答by Sinan Eldem

If

如果

git config --global core.filemode false

does not work for you, do it manually:

对您不起作用,请手动执行:

cd into yourLovelyProject folder

cd into .git folder:

cd 进入 .git 文件夹:

cd .git

edit the config file:

编辑配置文件:

nano config

change true to false

将真更改为假

[core]
        repositoryformatversion = 0
        filemode = true

->

->

[core]
        repositoryformatversion = 0
        filemode = false

save, exit, go to upper folder:

保存,退出,进入上层文件夹:

cd ..

reinit the git

重新启动 git

git init

you are done!

你完成了!

回答by Jakub Nar?bski

Adding to Greg Hewgill answer(of using core.fileModeconfig variable):

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

You can use --chmod=(-|+)xoption of git update-index(low-level version of "git add") to change execute permissions in the index, from where it would be picked up if you use "git commit" (and not "git commit -a").

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

回答by Tyler Long

You can configure it globally:

您可以全局配置它:

git config --global core.filemode false

git config --global core.filemode false

If the above doesn't work for you, the reason might be your local configuration overrides the global configuration.

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

Remove your local configuration to make the global configuration take effect:

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

git config --unset core.filemode

git config --unset core.filemode

Alternatively, you could change your local configuration to the right value:

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

git config core.filemode false

git config core.filemode false

回答by Kishor Vitekar

If you have used chmodcommand already then check the difference of file, It shows previous file mode and current file mode such as:

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

new mode : 755

新模式:755

old mode : 644

旧模式:644

set old mode of all files using below command

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

sudo chmod 644 .

sudo chmod 644 .

now set core.fileMode to false in config file either using command or manually.

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

git config core.fileMode false

then apply chmod command to change the permissions of all files such as

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

sudo chmod 755 .

and again set core.fileMode to true.

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

git config core.fileMode true

For best practises don't Keep core.fileMode false always.

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

回答by Ville

By definining the following alias (in ~/.gitconfig) you can easily temporarily disable the fileMode per git command:

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

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

When this alias is prefixed to the git command, the file mode changes won't show up with commands that would otherwise show them. For example:

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

git nfm status

回答by dryobs

If you want to set filemode to false in config files recursively (including submodules) : find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'

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

回答by Shashwat Gupta

Simple solution:

简单的解决方案:

Hit this Simple commandin project Folder(it won't remove your original changes)...it will only remove changesthat had been donewhile you changed project folder permission

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

command is below:

命令如下:

git config core.fileMode false

git 配置 core.fileMode 假

Why this all unnecessary file get modified:because you have changed the project folder permissions with commend sudo chmod -R 777 ./yourProjectFolder

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

when will you check changes what not you did? you found like below while using git diff filename

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

old mode 100644
new mode 100755