여러 파일 및 디렉토리 작업
이제 Git 캐시에서 단일 파일을 제거하는 방법을 알았으므로 여러 파일 또는 전체 디렉토리를 제거하는 것과 같은 더 복잡한 시나리오를 살펴보겠습니다.
연습을 위한 더 많은 파일 생성
먼저 연습을 위해 몇 개의 파일과 디렉토리 구조를 더 만들어 보겠습니다.
- 디렉토리와 몇 개의 추가 파일을 만듭니다.
## Create a directory for temporary files
mkdir temp
## Create some files in the temp directory
echo "This is a temporary file" > temp/temp1.txt
echo "Another temporary file" > temp/temp2.txt
## Create another log file in the main directory
echo "2023-01-02: System updated" > system.log
- 이러한 새 파일을 Git 추적에 추가합니다.
git add .
- 변경 사항을 커밋합니다.
git commit -m "Add temporary files and system log"
- Git 이 모든 파일을 추적하고 있는지 확인합니다.
git ls-files
다음과 같은 내용이 표시됩니다.
app.log
config.ini
sample.txt
system.log
temp/temp1.txt
temp/temp2.txt
Git 캐시에서 여러 파일 제거
이제 모든 로그 파일과 전체 temp 디렉토리를 Git 추적에서 제거하려는 경우를 가정해 보겠습니다.
- 로그 파일을 Git 추적에서 제거합니다.
git rm --cached system.log
- temp 디렉토리의 모든 파일을 재귀적으로 제거합니다.
git rm --cached -r temp/
-r 플래그는 Git 에 디렉토리의 모든 파일을 캐시에서 재귀적으로 제거하도록 지시하므로 여기에서 중요합니다.
- 상태를 확인합니다.
git status
로그 파일과 temp 디렉토리의 모든 파일이 Git 추적 시스템에서 삭제되도록 스테이징된 것을 볼 수 있습니다.
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: system.log
deleted: temp/temp1.txt
deleted: temp/temp2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
system.log
temp/
- 이러한 변경 사항을 커밋합니다.
git commit -m "Remove logs and temp directory from Git tracking"
- Git 이 더 이상 이러한 파일을 추적하지 않는지 확인합니다.
git ls-files
이제 다음만 표시됩니다.
app.log
config.ini
sample.txt
그러나 모든 파일은 여전히 로컬 디렉토리에 있습니다.
ls -la
ls -la temp/
.gitignore 를 사용하여 원치 않는 파일 추적 방지
이제 Git 추적에서 파일을 제거했으므로 실수로 다시 추가되는 것을 방지하기 위해 .gitignore 파일을 설정해 보겠습니다.
.gitignore 파일을 만듭니다.
nano .gitignore
- 다음 패턴을 파일에 추가합니다.
## Ignore log files
*.log
## Ignore temp directory
temp/
-
저장하고 종료합니다 (Ctrl+X, Y, Enter).
-
.gitignore 파일을 추가하고 커밋합니다.
git add .gitignore
git commit -m "Add .gitignore file"
이제 모든 파일을 Git 에 추가하려고 시도하더라도 .gitignore 파일을 존중하고 지정된 패턴을 추적하지 않습니다.
git add .
git status
로그 파일과 temp 디렉토리가 Git 추적에 추가되지 않는 것을 볼 수 있습니다.
이제 Git 캐시에서 여러 파일 및 디렉토리를 제거하고 .gitignore 파일을 사용하여 특정 파일이 향후 추적되지 않도록 하는 방법을 배웠습니다.