로컬 브랜치와 원격 브랜치 삭제하기

반응형

개요

git 브랜치는 원격저장소에 저장된 remote 브랜치와 현재 pc에 저장된 local 브랜치로 나뉜다.
개발이 완료되어 작업한 브랜치를 main 브랜치와 병합하였다면, 브랜치 정리를 위해서 작업한 local 브랜치와 remote 브랜치를 삭제하는 것이 좋다.
local 브랜치와 remote 브랜치를 삭제하는 방법을 알아보자

방법

아래의 명령어를 통해서 브랜치를 삭제할 수 있다.

local 브랜치

git branch --delete <branch name>

remote 브랜치

git push --delete origin <branch name>

예제

삭제하고자하는 브랜치를 확인한다.

git branch -a
main
* test-branch
  remotes/origin/main
  remotes/origin/test-branch

local 브랜치를 삭제한다.

git branch -D test-branch
test-branch 브랜치 삭제 (과거 7b2a27f).

local 브랜치가 지워졌는지 목록에서 확인해본다.

git branch -a
main
  remotes/origin/main
  remotes/origin/test-branch

remote 브랜치를 삭제한다.

git push --delete origin test-branch
To github.com:DeokHaengCho/git-test.git
 - [deleted]         test-branch

remote 브랜치가 지워졌는지 목록에서 확인해본다.

git branch -a
* main
  remotes/origin/main

주의사항

remote 브랜치 삭제를 시도하는데 remote 브랜치 경로를 찾을 수 없다는 에러가 발생하는 경우가 있다.
브랜치 목록을 확인하였을 때에는 원격 브랜치가 존재함에도 찾을 수 없다는 에러가 발생하는 경우이다.

git push origin -d test-branch
error: unable to delete 'test-branch': remote ref does not exist
error: failed to push some refs to '<git.path...>'

이는 기록되어있는 remote Repository가 실제와 일치하지 않아서 발생하는 문제이다.
이때는 fetch 명령어를 통해서 실제 remote Repository 의 정보가 반영되도록 갱신해야한다.

git fetch -p origin
From <git.path ...>
 - [deleted]           (none)     -> origin/test-branch
 - [deleted]           (none)     -> origin/test-branch2
 - [deleted]           (none)     -> origin/test-branch3
반응형