【发布时间】:2010-12-07 12:37:27
【问题描述】:
我有一个项目,我必须在开发时将 chmod 的文件模式更改为 777,但不应在主仓库中更改。
Git 接收chmod -R 777 . 并将所有文件标记为已更改。有没有办法让 Git 忽略对文件所做的模式更改?
【问题讨论】:
-
这对于在 Windows 上使用 git + 在 Windows 上的 Ubuntu 上使用 Bash 很有帮助
我有一个项目,我必须在开发时将 chmod 的文件模式更改为 777,但不应在主仓库中更改。
Git 接收chmod -R 777 . 并将所有文件标记为已更改。有没有办法让 Git 忽略对文件所做的模式更改?
【问题讨论】:
试试:
git config core.fileMode false
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 存储库或仅为一个 git 存储库设置此标志:
# this will set your the flag for your user for all git repos (modifies `$HOME/.gitconfig`)
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 在回购配置中显式设置 core.fileMode 为 true,如 Git global core.fileMode false overridden locally on clone 中所述
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,除非在非常罕见的环境中。
【讨论】:
git config --global core.filemode false,您只需为所有存储库执行一次。
git config 命令将设置写入正确的配置文件(.git/config 仅用于当前存储库,或者~/.gitconfig 如果与--global 一起使用)。
这可能有效:git config core.fileMode false
【讨论】:
简单的解决方案:
在项目文件夹中点击这个简单命令(它不会删除您原来的更改) ...它只会删除更改在您更改项目文件夹权限
时已完成命令如下:
git config core.fileMode false
为什么要修改所有不必要的文件: 因为您更改了项目文件夹权限 有表扬 sudo chmod -R 777 ./yourProjectFolder
你什么时候会检查你没有做过的改变? 您在使用 git diff filename
时发现如下old mode 100644
new mode 100755
【讨论】:
通过定义以下别名(在 ~/.gitconfig 中),您可以轻松地暂时禁用每个 git 命令的 fileMode:
[alias]
nfm = "!f(){ git -c core.fileMode=false $@; };f"
当此别名作为 git 命令的前缀时,文件模式更改不会与原本会显示它们的命令一起显示。例如:
git nfm status
【讨论】:
这对我有用:
find . -type f -exec chmod a-x {} \;
或反向,具体取决于您的操作系统
find . -type f -exec chmod a+x {} \;
【讨论】:
如果你已经使用过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。
【讨论】:
For best practises don't Keep core.fileMode false always 什么意思,你应该解释一下。
For best practises don't Keep core.fileMode false always. 某些文件系统(例如 FAT)不支持文件权限,因此操作系统会报告默认值(无论如何在我的系统上为 766)。在这种情况下,core.filemode 在本地配置中是绝对必要的,除非您想通过不必要和无意的权限更改来膨胀提交历史
core.filemode=false,那么 git 会忽略执行位的改变,不需要改变本地权限。除非您已经向索引添加了权限更改,否则您将错过关闭core.filemode 后需要git add 的步骤。
如果您想为所有存储库设置此选项,请使用 --global 选项。
git config --global core.filemode false
如果这不起作用,您可能使用的是较新版本的 git,因此请尝试 --add 选项。
git config --add --global core.filemode false
如果你在没有 --global 选项的情况下运行它并且你的工作目录不是一个 repo,你会得到
error: could not lock config file .git/config: No such file or directory
【讨论】:
--add,如git config --add --global core.filemode false
你可以全局配置:
git config --global core.filemode false
如果上述方法对您不起作用,原因可能是您的本地配置覆盖了全局配置。
移除你的本地配置,使全局配置生效:
git config --unset core.filemode
或者,您可以将本地配置更改为正确的值:
git config core.filemode false
【讨论】:
git config -l(列出当前配置 - 本地和全局)
如果
git config --global core.filemode false
不适合你,手动操作:
cd into yourLovelyProject folder
cd 到 .git 文件夹:
cd .git
编辑配置文件:
nano config
把真改成假
[core]
repositoryformatversion = 0
filemode = true
->
[core]
repositoryformatversion = 0
filemode = false
保存,退出,进入上层文件夹:
cd ..
重新启动 git
git init
你已经完成了!
【讨论】:
.git/config,只需在项目的根目录中添加一个简单的git config core.fileMode false 就足够了。如果您编辑配置文件,最好完全删除该指令,以便选择全局指令。
global 选项与手动编辑完全相同.git/config
~/.gitconfig 和 ~/project/.git/config 的歧义)
git init 我们应该将文件模式设置回 true 吗?
git init 将文件模式返回 TRUE!
如果你想在配置文件中递归地设置 filemode 为 false(包括子模块):
find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'
【讨论】:
git submodule foreach git config core.fileMode false
工作树中的撤消模式更改:
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
【讨论】:
xargs 中的 -d'\n' 部分,因为这是非法参数(并且不需要)。
tr '\n' '\0' 管道化,然后使用-0 arg 到xargs 以使用NUL 作为分隔符。
tr 成功了!这是 OSX 的完整命令:git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7-|tr '\n' '\0'|xargs -0 chmod -x
添加到Greg Hewgill answer(使用core.fileMode 配置变量):
您可以使用git update-index 的--chmod=(-|+)x 选项(“git add”的低级版本)来更改索引中的执行权限,如果您使用“git commit”(而不是“git commit -a”)。
【讨论】: