Git操作之 - 文件模式修改

本文介绍如何在Ubuntu Git环境下,避免Windows系统修改代码文件时自动添加可执行属性。通过理解gitconfig的fileMode设置,指导读者如何将它设置为false,确保提交时不包含不必要的权限变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时工作中,经常是在Ubuntu虚拟机下使用Git管理代码。

同时为了方便编辑,在Ubuntu上架设了Samba服务器,这样就能在Windows上访问Git代码并进行编辑。

但由于Ubuntu和Windows的文件系统操作不同,一个本来是仅有读写属性的源码文件,在Windows上修改后,就自动加上了可执行属性。

文件修改前:

$ ll mySrc.cpp

-rw-rw-r-- 1 shawn shawn 0 3月  24 09:57 mySrc.cpp

文件修改后:

$ ll mySrc.cpp

-rwxrw-r-- 1 shawn shawn 1 3月  24 09:57 mySrc.cpp*

这时如果不小心进行了提交,就会把文件的可执行属性一并提交到服务器。

一旦被爱挑毛病的老板发现,后果你懂的。

那如何能避免这个问题呢?

在git的功能设计中,已经考虑到了这种情况,在git config中有相应的设置项,如下所示,查询当前的设置项的值:

$ git config core.fileMode

true

当fileMode选项为true时,就会发生如上所述问题。我们只需把fileMode改为false即可。

$ git config core.fileMode false

注意:

执行git config时要在当前的repository内,这时配置的是local setting。

在repository之外,可以配置global或system setting。

$ git config --global core.fileMode false

$ git config --system core.fileMode false

附, 在git帮助文档中关于fileMode的说明:

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) 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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值