git tips: 设置filemode,避免NTFS文件权限变更引起的修改

96
代码厨子
2016.04.07 21:35* 字数 479

如果你也和我一样,使用双(甚至是多)系统。平时使用Linux来进行工作。偶尔使用Windows进行娱乐。那就会有文件共享的问题。

我把数据盘格式化为NTFS格式。以方便在Linux和Window系统之间进行数据共享。在使用GIT工作时,有时候就发现,新Clone下来的项目,在没有做任何修改的前提下,git status命令提示几乎所有文件都有修改。这就造成无法进行后续的git pull更新操作。使用git pull -f参数也没有。

如下图所示:

$ git status
    modified:   service-loadbalancer/test-samples/TestDefaultCustomAlgorithm.cfg
    modified:   service-loadbalancer/test-samples/TestServiceAffinity.cfg
    modified:   service-loadbalancer/test-samples/TestServiceAffinityWithCookies.cfg
    modified:   service-loadbalancer/test-samples/TestSvcCustomAlgorithm.cfg
    modified:   service-loadbalancer/test-samples/TestSyslog.cfg
    modified:   service-loadbalancer/test-samples/loadbalancer_test.json
    modified:   test-utils/.gitignore
    modified:   test-utils/README.md
    modified:   test-utils/utils/utils.go
...
...
$ git diff test-utils/utils/utils.go
diff --git a/test-utils/utils/utils.go b/test-utils/utils/utils.go
old mode 100644
new mode 100755

原始文件并没有修改,只是文件的权限被修改了。原因是NTFS没有Linux下丰富的权限设置。clone下来的文件都被设置成了所有人员可读写。

$ ll test-utils/utils/utils.go 
-rwxrwxrwx 1 root root 3597 Apr  7 09:23 test-utils/utils/utils.go

而且通过chmod修改权限也不会生效。

$ chmod 644 test-utils/utils/utils.go 
$ ll test-utils/utils/utils.go 
-rwxrwxrwx 1 root root 3597 Apr  7 09:23 test-utils/utils/utils.go

总不能把文件系统重新格式化了再用吧 :-(

既然是文件权限的问题,而且文件只是权限发生了变化。那git有没有可以忽略这种权限变化的选项呢?

答案当然是YES

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 an non-executable file with executable bit on. [git-clone(1)]
    (https://www.kernel.org/pub/software/scm/git/docs/git-clone.html) or [git-init(1)]
    (https://www.kernel.org/pub/software/scm/git/docs/git-init.html) 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)]
    (https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html).

    The default is true (when core.filemode is not specified in the config file).

通过在仓库中设置filemode为false。就可以解决问题:


$ git config  core.filemode false
$ git st
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

或者,如果你嫌麻烦,也可以把这个设置配置到全局配置中。

$ git config --global core.filemode false

不过要注意,设置全局的配置有一点点安全风险性。如果某个文件被错误的配置了可执行权限,或者某些文件的可读写权限没有适当的配置。则在Linux系统下程序被安装后,可能会引起错误。原代码之类的,问题还不大。对于脚本、二进制程序权限还是明确配置了比较好。

开发者
Web note ad 1