무시된 파일 확인과 결합
이전 단계에서 git status 및 git ls-files --others를 사용하여 추적되지 않은 파일을 식별하는 방법을 배웠습니다. 그러나 때로는 임시 파일, 빌드 출력 또는 민감한 정보가 포함된 구성 파일과 같이 Git 이 의도적으로 추적하지 않으려는 파일이 있습니다. Git 을 사용하면 이러한 파일을 .gitignore 파일에 지정할 수 있습니다.
이 단계에서는 .gitignore 파일을 만들고 git status 및 git ls-files --others의 출력에 어떤 영향을 미치는지 살펴보겠습니다.
먼저, ~/project/my-time-machine 디렉토리에 있는지 확인하십시오.
이제 무시하려는 파일을 만들어 보겠습니다. temp.log라고 부르겠습니다.
echo "This is a temporary log file" > temp.log
git status를 다시 실행합니다.
git status
notes.txt와 temp.log가 모두 추적되지 않은 파일로 나열되는 것을 볼 수 있습니다.
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: message.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
notes.txt
temp.log
no changes added to commit (use "git add" and/or "git commit -a")
이제 .gitignore 파일을 만들고 temp.log를 추가해 보겠습니다. nano 편집기를 사용하여 파일을 만들고 편집합니다.
nano .gitignore
nano 편집기 내에서 다음 줄을 입력합니다.
temp.log
Ctrl + X를 누른 다음 Y를 눌러 저장하고 Enter를 눌러 파일 이름을 확인합니다.
이제 git status를 다시 한 번 실행합니다.
git status
이번에는 temp.log가 "Untracked files:" 목록에 더 이상 나타나지 않아야 합니다.
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: message.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
notes.txt
no changes added to commit (use "git add" and/or "git commit -a")
Git 은 이제 temp.log를 무시하도록 알고 있습니다. git ls-files --others가 어떻게 영향을 받는지 살펴보겠습니다.
git ls-files --others
출력은 이제 notes.txt만 표시해야 합니다.
notes.txt
기본적으로 git ls-files --others는 무시된 파일을 나열하지 않습니다. 일반적으로 Git 에 명시적으로 무시하도록 지시한 파일을 보고 싶지 않으므로 이것이 원하는 동작입니다.
다른 추적되지 않은 파일과 함께 무시된 파일을 보고 싶다면 git ls-files --others와 함께 --ignored 옵션을 사용할 수 있습니다.
git ls-files --others --ignored
출력에는 이제 추적되지 않은 파일과 무시된 파일이 모두 포함됩니다.
.gitignore
notes.txt
temp.log
.gitignore 자체는 추가하고 커밋할 때까지 추적되지 않은 파일입니다.
.gitignore를 사용하는 방법을 이해하는 것은 저장소를 깨끗하게 유지하고 실제 프로젝트 파일에 집중하는 데 매우 중요합니다. 버전 관리에 포함되지 않아야 하는 파일의 실수로 인한 커밋을 방지합니다.