스택큐힙리스트

Git에서 파일 모드 (chmod) 변경을 무시하는 방법은 무엇인가요? 본문

카테고리 없음

Git에서 파일 모드 (chmod) 변경을 무시하는 방법은 무엇인가요?

스택큐힙리스트 2023. 3. 16. 20:48
반응형

제가 개발 중인 프로젝트에서는 chmod 로 끝나는 파일의 모드를 777로 변경해야하지만 메인 저장소에서는 변경되지 않아야합니다.

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 플래그는 일회성 명령에 대해 이 옵션을 설정하는 데 사용할 수 있습니다.

git -c core.fileMode=false diff

-c core.fileMode=false을 타이핑하는 것은 귀찮을 수 있으므로 모든 git 리포지토리 또는 하나의 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를 명시적으로 true로 설정합니다.

경고

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를 사용할 필요가 없을 것입니다.

답변 2

Git로 파일을 관리할 때 종종 파일 권한(mode)이 변경될 수 있습니다. 이러한 파일 권한 변경은 파일의 내용 변경과 같이 Git의 커밋(commit) 히스토리에 기록됩니다. 그러나 때로는 이 파일 권한 변경을 무시하고 Git이 커밋 히스토리에 반영하지 않도록 해야하는 경우가 있습니다.

Git에서 파일 권한 변경을 무시하는 방법은 .gitignore 파일을 사용하는 것입니다. .gitignore 파일은 Git이 무시해야하는 파일 및 디렉토리 목록을 지정하는 파일입니다. 이 파일을 사용하여 Git이 파일 권한 변경을 무시하도록 할 수 있습니다.

먼저 .gitignore 파일을 만들어야 합니다. 이 파일은 Git 저장소의 루트 디렉토리에 위치해야 합니다. 파일 이름은 반드시 '.gitignore'로 지정해야 합니다.

.gitignore 파일에는 무시해야하는 파일 및 디렉토리 목록을 지정할 수 있습니다. 파일 권한 변경을 무시하도록 하려면 다음과 같이 .gitignore 파일에 다음과 같은 줄을 추가합니다.

```

# Ignore file mode (chmod) changes

*/*/*/mode

```

위 예제에서는 'mode'라는 파일 이름을 가진 파일에서 발생하는 변경 사항을 무시하도록 .gitignore 파일에 추가하였습니다. 이것은 파일 권한(mode) 변경에 대한 것으로 변경된 파일의 파일 모드만 다루고 모든 파일이나 폴더에 대해 변경하지 않습니다.

이렇게 하면 Git이 파일 권한 변경에 대한 커밋 히스토리를 무시하도록 하여 Git이 파일 권한 변경에 영향 받지 않도록 할 수 있습니다. 이를 통해 Git을 더욱 체계적이고 구조화된 방식으로 사용하여 파일 변경에 대한 효율성을 높일 수 있습니다.

반응형
Comments