브랜치(Branch)는 "독립된 평행 우주를 만드는 작업"입니다. 메인 코드베이스를 건드리지 않고 안전하게 새 기능을 실험하고 개발할 수 있는 핵심 메커니즘입니다. 이 문서는 프롬프트 상의 브랜치 표시(branch-name) $를 명시하여 최신 표준인 git switch를 기준으로 실무 브랜치 운영법을 다룹니다.
1. 최신 명령어: checkout 대신 switch를 쓰자
- 브랜치 전환 / 생성:
git switch사용 - 파일 변경 취소 / 복구:
git restore사용
2. 브랜치 생성 및 이동 기본 명령어
# 1. 로컬 및 원격 브랜치 전체 목록 확인
(main) $ git branch -a
* main
remotes/origin/main
remotes/origin/develop
# 2. 새로운 브랜치를 만들고 즉시 이동 (-c: create)
(main) $ git switch -c feature/shopping-cart
Switched to a new branch 'feature/shopping-cart'
# 프롬프트가 (feature/shopping-cart)로 바뀜
(feature/shopping-cart) $ git status
# 3. 기존에 있던 브랜치로 이동
(feature/shopping-cart) $ git switch develop
Switched to branch 'develop'
# 4. 직전에 머물렀던 이전 브랜치로 빠르게 되돌아가기
(develop) $ git switch -
Switched to branch 'feature/shopping-cart'
(feature/shopping-cart) $ 3. 다 쓴 브랜치 삭제: -d vs -D의 차이
# develop 브랜치로 이동 후
(feature/shopping-cart) $ git switch develop
Switched to branch 'develop'
# 안전한 삭제 (-d: 이미 머지된 경우에만 삭제 허용)
(develop) $ git branch -d feature/shopping-cart
Deleted branch feature/shopping-cart (was 3a1f9b2).
# 머지되지 않은 실험 브랜치 강제 폐기 (-D)
(develop) $ git branch -D feature/abandoned-experiment
Deleted branch feature/abandoned-experiment.