Git 추적되지 않은 파일

Stewart Nguyen 2022년8월23일
Git 추적되지 않은 파일

이 기사에서는 Git에서 파일을 추적 해제하는 방법을 소개합니다.

git 저장소 내의 파일에는 추적됨 또는 추적되지 않음의 두 가지 상태가 있습니다.

추적 파일은 Git이 알고 있는 파일입니다.

추적되지 않은 파일은 작업 저장소 내에서 생성되었지만 git add 명령을 사용하여 추가되지 않은 파일입니다.

이 시나리오를 고려하십시오.

cd ~
mkdir my-repo
cd my-repo
git init
touch file.txt
git add file.txt
git commit -m 'First commit'

Git은 file.txt에 대해 알고 있으므로 기술적으로 file.txt가 추적됩니다.

나중에 이 파일 이름을 .gitignore에 추가하여 file.txt(또는 실수로 커밋된 모든 파일)를 무시하도록 Git에 지시하고 싶습니다.

touch .gitignore
echo 'file.txt' >> .gitignore
git add .gitignore && git commit -m 'Ignore file.txt'

무슨 일이 일어날 지?

.gitignore를 커밋한 후 file.txt를 변경하면 git은 여전히 ​​저장소 인덱스에 존재하기 때문에 file.txt가 추적됨을 표시합니다.

$ echo 'qwe' > file.txt
$ git status
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")

Git에서 파일 추적 해제

1단계, 다음 명령을 실행합니다.

$ git rm --cache file.txt
rm 'file.txt'
$ git st
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    file.txt
  • rm은 추적을 중지하고 로컬 저장소 디렉토리에서 파일을 제거합니다.
  • --cache 옵션은 rm 명령이 인덱스에서만 파일을 제거하고 로컬 저장소에서는 파일을 제거하지 않도록 지정합니다.

git rm --cache file.txt는 저장소 인덱스에서 파일을 제거하여 file.txt 추적을 중지하지만 파일은 그대로 유지합니다.

$ git commit -m 'Remove file.txt from tracking'
[master 4697164] Remove file.txt from tracking
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 file.txt

이제부터 Git은 file.txt에 대한 변경 사항을 추적하지 않습니다.

$ echo '123' > file.txt
$ git st
On branch master
nothing to commit, working tree clean

관련 문장 - Git Tracking