# git gone - 원격에서 삭제된 브랜치를 모두 삭제하는 커스텀 명령어

> Clean Markdown view of GeekNews topic #7933. Use the original source for factual precision when an external source URL is present.

## Metadata

- GeekNews HTML: [https://news.hada.io/topic?id=7933](https://news.hada.io/topic?id=7933)
- GeekNews Markdown: [https://news.hada.io/topic/7933.md](https://news.hada.io/topic/7933.md)
- Type: news
- Author: [studroid](https://news.hada.io/@studroid)
- Published: 2022-12-01T10:45:24+09:00
- Updated: 2022-12-01T10:45:24+09:00
- Original source: [erikschierboom.com](https://www.erikschierboom.com/2020/02/17/cleaning-up-local-git-branches-deleted-on-a-remote/)
- Points: 17
- Comments: 0

## Topic Body

원격(remote) 브랜치를 트래킹하는 로컬 브랜치가 존재하는 상태에서, 원격 브랜치가 삭제된 경우, 내 컴퓨터에만 남게 된 로컬 브랜치를 지워주는 커스텀 명령어 설정 방법  
  
- 트래킹하는 상태에서 원격 브랜치가 삭제되면 `[gone]` 상태로 전환된다는 점을 이용함  
- `git gone` 명령어는 두 가지를 실행함  
   1. `git fetch -p`를 통해 prune 처리 (지워진 원격 브랜치 정보 갱신)  
   2. 지워진 원격 브랜치를 트래킹하는 로컬 브랜치를 모두 삭제  
- 트래킹하는 원격 브랜치가 없는 로컬 브랜치는 지워지지 않음  
  
git 실행이 가능한 터미널에서 아래 명령어를 사용하면 `git gone` 명령어를 커스텀 커맨드로 추가할 수 있음  
  
- `git config --global alias.gone "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"`

## Comments



_No public comments on this page._
