개발 Dev/깃 Git

git에 이미 추가했던 파일을 더 이상 추적하지 않아 저장소에 저장되지 않도록 하면서, 파일은 남겨 두고 싶다면?

Tap to restart 2021. 1. 27. 18:00
반응형

git -rm 을 사용하면 된다

예를 들어 app.yaml 파일이 이미 git에 추가된 상태이다.

app.yaml 파일을 삭제하지 않고 남겨 두면서 git에서 추적되지 않게 하고 싶다.

.gitignore 파일에 뒤늦게 app.yaml을 추가해도 이미 추가했던 파일이면 적용되지 않는다.

 

1) git -rm app.yaml

이렇게 하면 app.yaml 파일이 삭제된다.

 

2) git -rm --cached app.yaml

이렇게 하면 app.yaml 파일이 삭제되지 않고 남아 있다. 더 이상 git에서 추적되지 않게 된다.

 

--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

 

--cached에 대한 설명이다. 더이상 추적되지 않고(unstage) 남아 있게 될 것(will be left)란 것을 알 수 있다.

이게 바로 내가 원하는 것이다.

 

이렇게 하고나서 원격저장소에 푸쉬push하게 되면 원격저장소 상에는 app.yaml 파일이 삭제된다. 그리고 로컬 내 작업 폴더에는 파일이 남아 있게 된다.

 

 

git -rm 다른 옵션이 궁금하다면 git -rm --help로 매뉴얼을 볼 수 있다.



NAME
       git-rm - Remove files from the working tree and from the index

SYNOPSIS
       git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>...

DESCRIPTION
       Remove files from the index, or from the working tree and the index.
       git rm will not remove a file from just your working directory. (There
       is no option to remove a file only from the working tree and yet keep
       it in the index; use /bin/rm if you want to do that.) The files being
       removed have to be identical to the tip of the branch, and no updates
       to their contents can be staged in the index, though that default
       behavior can be overridden with the -f option. When --cached is given,
       the staged content has to match either the tip of the branch or the
       file on disk, allowing the file to be removed from just the index.

OPTIONS
       <file>...
           Files to remove. Fileglobs (e.g.  *.c) can be given to remove all<file>...
           Files to remove. Fileglobs (e.g.  *.c) can be given to remove all
           matching files. If you want Git to expand file glob characters, you
           may need to shell-escape them. A leading directory name (e.g.  dir
           to remove dir/file1 and dir/file2) can be given to remove all files
           in the directory, and recursively all sub-directories, but this
           requires the -r option to be explicitly given.

       -f, --force
           Override the up-to-date check.

       -n, --dry-run
           Don't actually remove any file(s). Instead, just show if they exist
           in the index and would otherwise be removed by the command.

       -r
           Allow recursive removal when a leading directory name is given.

       --
           This option can be used to separate command-line options from the
           list of files, (useful when filenames might be mistaken for
           command-line options).

       --cached
           Use this option to unstage and remove paths only from the index.
           Working tree files, whether modified or not, will be left alone.

       --ignore-unmatch
           Exit with a zero status even if no files matched.

       -q, --quiet
           git rm normally outputs one line (in the form of an rm command) for
           each file removed. This option suppresses that output.


Figure 8. 파일의 라이프사이클. (출처: Git의 기초 - 수정하고 저장소에 저장하기)

 

참고: Git의 기초 - 수정하고 저장소에 저장하기

반응형