我有一个项目,在开发过程中需要使用 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
在所有的Git存储库中或仅在一个Git存储库中设置-c core.fileMode=false
标志,可以避免不必要的麻烦:
# this will set your the flag for your user for all git repos (modifies `$HOME/.gitconfig`)
# WARNING: this will be override by local config, fileMode value is automatically selected with latest version of git.
# This mean that if git detect your current filesystem is compatible it will set local core.fileMode to true when you clone or init a repository.
# Tool like cygwin emulation will be detected as compatible and so your local setting WILL BE SET to true no matter what you set in global setting.
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
,那么你只需要在所有仓库中执行一次即可。 - Greggit config
命令会将设置写入正确的配置文件中(对于当前仓库来说是 .git/config
,如果使用 --global
则是 ~/.gitconfig
)。 - Greg Hewgill撤消工作树中的模式更改:
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
或在 BSD/macOS 上
git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x
git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x
xargs
中省略-d'\n'
部分,因为这是不合法的参数(也不需要)。 - Pascaltr '\n' '\0'
命令,然后使用 -0
参数将 NUL 作为分隔符传递给 xargs 命令。 - Marktr
命令起作用了!以下是适用于OSX的完整命令:git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7-|tr '\n' '\0'|xargs -0 chmod -x
。 - K.-Michael Aye--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
--add
,例如 git config --add --global core.filemode false
。该命令用于将 core.filemode
设置为 false
并将其添加到全局配置中。 - mgaertgit config --global core.filemode false
如果这对你不起作用,那么请手动操作:
cd into yourLovelyProject folder
进入 .git 文件夹:
cd .git
编辑配置文件:
nano config
将 true 改为 false
[core]
repositoryformatversion = 0
filemode = true
->
[core]
repositoryformatversion = 0
filemode = false
保存、退出、回到上一个文件夹:
cd ..
重新初始化Git。git init
你已经完成了!
.git/config
文件,只需在项目根目录下运行git config core.fileMode false
即可。如果您要编辑配置文件,最好将指令完全删除,以便选择全局设置。 - Felix~/.gitconfig
和~/project/.git/config
) - ddavisongit init
,我们应该将 filemode 设置回 true 吗? - Jordan在Greg Hewgill的回答中提到(使用core.fileMode
配置变量),你还可以使用git update-index的 --chmod=(-|+)x
选项(低级版本的 "git add")来更改索引中的执行权限,如果您使用“git commit”(而不是“git commit -a”),它将从其中获取这些更改。
git config --global core.filemode false
如果上述方法对您无效,则可能是因为您的本地配置覆盖了全局配置。git config --unset core.filemode
或者,您可以将本地配置更改为正确的值:
< p > git config core.filemode false
git config -l
(列出当前配置 - 包括本地和全局)。 - Krzysztof Bociurkosudo 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。
core.filemode=false
,那么git将忽略执行位的更改,无需更改本地权限。除非您已经将权限更改添加到索引中,否则您需要在关闭 core.filemode
后执行 git add
步骤。 - KevinOrrgit config core.fileMode false
,会发生什么。在设置之后,某些文件将不会被视为已修改(只有权限更改的文件),而其他一些文件则由于代码更改等其他更改而仍然被视为已修改。如果我提交这些文件,那么修改后的权限是否也会作为其一部分被提交? - Raja Ehteshamgit add
添加的文件和在运行git status
时看到的更改)。 - KevinOrr通过在~/.gitconfig文件中定义以下别名,您可以轻松地在每个git命令中暂时禁用filemode:
通过在~/.gitconfig文件中定义以下别名,您可以轻松地在每个git命令中暂时禁用filemode:
[alias]
nfm = "!f(){ git -c core.fileMode=false $@; };f"
当这个alias作为git命令的前缀时,文件模式的更改不会显示在本来会显示它们的命令中。例如:
git nfm status
find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'
git submodule foreach git config core.fileMode false
。 - courtlandj简单解决方案:
在项目文件夹中运行此简单命令(它不会删除您的原始更改)......它只会删除更改,这些更改是在您更改项目文件夹权限时进行的。
命令如下:
git config core.fileMode false
为什么所有这些不必要的文件都被修改了:因为您使用以下命令更改了项目文件夹权限:sudo chmod -R 777 ./yourProjectFolder
当您检查未更改的内容时,会发现像下面这样,使用git diff filename
old mode 100644
new mode 100755
git diff
操作中忽略权限更改,而又不希望修改Git配置文件,可以按照Zed的回答这里所述,使用git diff -G.
命令。 - user82216