반응형
새로운 파일을 추가하거나 코드를 수정하거나 파일을 삭제하는 등 다양한 활동을 통해 개발을 진행한다. 이 때 더이상 이 변경사항을 유지하고 싶지 않은 경우 많은 개발자들은 아래 명령어로 변경사항을 초기화 한다.
git reset --hard
reset 명령어는 이미 git에서 관리하는 파일들에 한하여 변경사항을 초기화한다. 하지만 reset 명령어로도 untracked file 은 정리되지 않고 유지된다. untracked file을 제거하려면 clean 명령어를 사용해야 한다.
$ git clean -h
usage: git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...
-q, --quiet do not print names of files removed
-n, --dry-run dry run
-f, --force force
-i, --interactive interactive cleaning
-d remove whole directories
-e, --exclude <pattern>
add <pattern> to ignore rules
-x remove ignored files, too
-X remove only ignored files
예제를 위해 아래와 같이 설정된 환경이라고 하자. Untracked file로 test_dir 디렉토리와 test_file 파일이 존재한다. test_dir 내에는 test_file_in_dir 란 이름의 파일이 존재한다.
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_dir/
test_file
git clean -n (dry run)
dry run 옵션은 clean 명령어를 실행했을 때 삭제가 될 파일의 리스트를 보여준다. 아래와 같이 test_file 이 삭제될거라는 메시지를 확인할 수 있다.
$ git clean -n
Would remove test_file
-n 옵션만 사용시에는 test_dir 디렉토리까지 출력은 되지 않았다. -n 옵션 사용 시 파일에 한정된다. 디렉터리까지 확인을 위해서는 d 옵션을 추가해준다.
$git clean -nd
Would remove test_dir/
Would remove test_file
삭제하기 전에 어떤 파일이 삭제될지 유지해야 할 파일이 있는지 미리 파악하기 위해서 dry run 옵션을 꼭 실행해보자.
clean 명령어로 삭제해보기
-f (force) 옵션을 사용하여 untracked files 들을 삭제한다. git clean -f 뒤에 디렉터리 경로를 명시하면 명시한 디렉터리와 디렉터리내에 파일까지 삭제한다.
$git clean -f # 오직 파일만 삭제된다.
Removing test_file
$git clean -fd # untracted 디렉터리도 함께 삭제
$ git clean -fd
Removing test_dir/
반응형
'ETC > git' 카테고리의 다른 글
[Github] Rest API 정리하기 (0) | 2022.08.23 |
---|---|
[git] fatal: The remote end hung up unexpectedly (0) | 2019.06.21 |
자주 사용하는 git alias 정리 (0) | 2018.07.05 |
[Github] remote repository와의 sync 맞추기 (0) | 2018.04.20 |
Linux terminal cmd line에 branch name 넣기 (0) | 2017.08.04 |