Branch 관련 명령어
✒️ 2025-07-26 20:57 내용 수정
Branch 확인
1. Branch 목록 보기
- 현재 로컬 저장소에 존재하는 Branch 목록을 보여줌
git branch
- 원격 Branch 목록 확인
git branch -r
- 원격+로컬 저장소 Branch 확인
- 참고 자료 : Git remote branch 가져오기
git branch -a
2. 원격 Branch 내용 동기화
- 원격 Branch의 최신 Branch 내용을 받아옴
- 원격 Branch에 새 Commit이 들어왔을 때 로컬에서 동기화
git fetch
- 원격 저장소에서 삭제된 Branch를 로컬 원격 추적 Branch에서 자동으로 제거하면서
fetch를 수행
git fetch --prune
- 원격 Branch와 로컬 Branch를 비교하여 원격에 없는 Branch를 정리
git remote prune origin
3. 현재 Branch 확인
git branch --show-current
Branch 변경
- 기존 방식
git checkout <Branch이름>
- v2.23+ 이상 최신 방식(권장)
git switch <Branch이름>
- 이전 Branch로 돌아가기
git switch -
Branch 이름 변경
git branch -m <변경할Branch이름> <새Branch이름>
Branch 생성
1. Branch 생성
- 새로운 Branch를 생성하지만 해당 Branch로 전환되지는 않음
git branch <Branch이름>
git branch feature/login
- 원본 Branch로부터 새 Branch 생성
git branch <Branch이름> <원본Branch이름>
2. Branch 생성 + 전환 (checkout)
- Branch를 생성하고 바로 해당 Branch로 전환
git checkout -b <Branch이름>
git checkout -b feature/login
- v2.23v 최신 버전
git switch -c <Branch이름>
3. 원격에 있는 Branch를 로컬에 가져와 Branch 생성하기
git checkout -t origin/<Branch이름>
4. 원격 Branch 기반으로 Branch 생성
git checkout -b <새로운Branch이름> origin/<기존Branch이름>
git checkout -b feature/login origin/develop
5. 생성한 Branch 업로드
git push
- 새 Branch에
--set-upstream을 적용하여 원격으로 전송할 때
git push -u origin <Branch이름>
-
로컬의 현재 Branch가 원격 Branch보다 뒤쳐졌을 때 강제로 원격 Branch로 push하기
# 에러 내용 hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. If you want to integrate the remote changes, hint: use 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.--force실행
git push origin master -f
Branch 병합 (Merge vs Rebase)
1. Merge
- 두 Branch를 병합하여 병합 Commit 생성
- 원래 Commit history 유지
git merge feature
- Branch 통합할 때 원본 Branch들이 같은 경우에 사용 가능
git merge <병합할 Branch이름>
- origin이 다른 두 Branch 병합
refusing to merge unrelated histories에러 시 사용- 참고 자료 : Git push가 안되는 경우
git pull origin <병합할 Branch> --allow-unrelated-histories
2. Rebase
- Commit을 다른 Branch 기반으로 재정렬
- Commit history가 깔끔하다.
- 협업 중인 Branch에는 주의
git rebase newbranch
Branch 삭제
1. 로컬 Branch 삭제
- 병합된 Branch만 삭제 가능
git branch -d <Branch이름>
- 병합 여부 상관없이 강제 삭제
git branch -D <Branch이름>
2. 원격 Branch 삭제
git push origin --delete <Branch이름>