Git でファイルモード (chmod) の変更を無視するにはどうすればいいですか? 質問する

Git でファイルモード (chmod) の変更を無視するにはどうすればいいですか? 質問する

開発中にファイルのモードをchmod777 に変更する必要があるプロジェクトがありますが、メイン リポジトリでは変更しないでください。

Git はすべてのファイルを検出しchmod -R 777 .、変更済みとしてマークします。ファイルに加えられた変更を Git が無視するようにする方法はありますか?

ベストアンサー1

試す:

git config core.fileMode false

からgit-config(1):

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フラグは、1 回限りのコマンドに対してこのオプションを設定するために使用できます。

git -c core.fileMode=false diff

入力するのは-c core.fileMode=false面倒なので、このフラグをすべての git リポジトリに対して設定することも、1 つの git リポジトリに対してのみ設定することもできます。

# 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に設定すると、trueGit グローバル 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非常にまれな環境を除いて、 を使用する必要がなくなります。

おすすめ記事