我有一个项目,我必须在其中更改文件的模式 chmod
开发时到777,但主要回购不应该改变。
Git接受了 chmod -R 777 .
并将所有文件标记为已更改。有没有办法让Git忽略对文件进行的模式更改?
我有一个项目,我必须在其中更改文件的模式 chmod
开发时到777,但主要回购不应该改变。
Git接受了 chmod -R 777 .
并将所有文件标记为已更改。有没有办法让Git忽略对文件进行的模式更改?
尝试:
git config core.fileMode false
从 GIT-配置(1):
core.fileMode If false, the executable bit differences between the index and the working copy are ignored; useful on broken filesystems like FAT. See git-update-index(1). True by default.
该 -c
flag可用于为一次性命令设置此选项:
git -c core.fileMode=false diff
而且 --global
flag将使其成为登录用户的默认行为。
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
,除了非常罕见的环境。
工作树中的undo模式更改:
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
如果要为所有回购设置此选项,请使用 --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
如果
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
你完成了!
添加到 Greg Hewgill回答 (使用 core.fileMode
配置变量):
您可以使用 --chmod=(-|+)x
的选择 git update-index (低级版本的“git add”)更改索引中的执行权限,如果使用“git commit”(而不是“git commit -a”),将从中获取执行权限。
您可以全局配置它:
git config --global core.filemode false
如果上述方法不适合您,原因可能是您的本地配置会覆盖全局配置。
删除本地配置以使全局配置生效:
git config --unset core.filemode
或者,您可以将本地配置更改为正确的值:
git config core.filemode false
如果你已经使用过 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。
如果要在递归的配置文件中将filemode设置为false(包括子模块):
find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'
通过定义以下别名(在〜/ .gitconfig中),您可以轻松地临时禁用fileMode per git命令:
nfm = "!f(){ git -c core.fileMode=false $@; };f"
当此别名以git命令作为前缀时,文件模式更改将不会显示将显示它们的命令。例如:
git nfm status