Git

원격에 있는 Branch 명 바꾸기

scone 2024. 1. 25. 02:33

https://github.com/boostcampaitech6/level2-objectdetection-cv-03

 

GitHub - boostcampaitech6/level2-objectdetection-cv-03: level2-objectdetection-cv-03 created by GitHub Classroom

level2-objectdetection-cv-03 created by GitHub Classroom - GitHub - boostcampaitech6/level2-objectdetection-cv-03: level2-objectdetection-cv-03 created by GitHub Classroom

github.com

 

Boostcamp에서 저희 팀은 GitHub Flow 전략을 채택하였고

Git Convention을 정하였습니다.

 

그러나 실수로 Branch 명을 잘못 적는 일이 생겼고, 이를 뒤늦게 고치려고 합니다. ( 하이픈 쓰기로 했는데 언더바 씀... )

Git Graph에 어떠한 변경을 가하지 않고도 Branch 명만 바꿀 수 있는데요

이는 Git 에서 Branch는 특정 커밋을 가리키는 포인터에 불과하기 때문 입니다.

 

  • 로컬에서 원격 브랜치를 추적하는 브랜치를 생성하고 체크아웃 합니다.
git fetch
git checkout -b <로컬-브랜치-이름> origin/<기존-원격-브랜치-이름>

 

브랜치 이름이 잘 기억나지 않는다면

git branch -r

다음과 같이 -r 옵션을 주면 remote 에서의 git branch 명이 모두 뜹니다.

 

  • 로컬 브랜치의 이름을 변경합니다.
git branch -m <새-로컬-브랜치-이름>
  • 새로운 이름으로 원격 브랜치를 생성합니다.
git push origin <새-로컬-브랜치-이름>
  • 기존의 원격 브랜치를 삭제합니다.
git push origin --delete <기존-원격-브랜치-이름>
  • 로컬에서 브랜치 추적 정보를 업데이트 합니다.
git branch -u origin/<새-로컬-브랜치-이름>

 

반드시 팀 구성원들에게도 변경사항을 알려야겠죠.

 

 

적용 예시

git checkout -b feat-#47/rename_newmm_to_mmdetection_v3 origin/feat-#47/rename_newmm_to_mmdetection_v3

바꾸고자 하는 브랜치로 체크아웃

 

git branch -m feat-#47/rename-newmm-to-mmdetection-v3

origin에는 기존 이름의 branch가 그대로 남고 local에서의 branch 이름이 바뀌었습니다.

 

git push origin feat-#47/rename-newmm-to-mmdetection-v3

 

git push origin --delete feat-#47/rename_newmm_to_mmdetection_v3

 

git branch -u origin/feat-#47/rename-newmm-to-mmdetection-v3

로컬 그래프에 반영

 

정리

git checkout -b <로컬-브랜치-이름> origin/<기존-원격-브랜치-이름>

git branch -m <새-로컬-브랜치-이름>

git push origin <새-로컬-브랜치-이름>

git push origin --delete <기존-원격-브랜치-이름>

git branch -u origin/<새-로컬-브랜치-이름>