DevOps/Git

[Git] Local, Remote branch 이름 변경

sol_git 2025. 1. 14. 15:56

👉🏻 Local branch 이름 변경

  1. 변경할 브랜치로 이동
$ git checkout 브랜치명

$ git checkout old-branch
  1. 로컬브랜치 이름 변경
$ git branch -m 바꿀브랜치명

$ git branch -m new-branch
$ --> old-branch가 new-branch로 이름이 변경됨!

old-branch에 commit/push 히스토리가 있어도 상관 없다!
해당 히스토리를 그대로 간직한 채로 branch 이름만 변경된 것!


👉🏻 Remote branch 이름 변경

이미 Github, Gitlab에 Push한 branch의 경우,
로컬에서 이름 바꾼 브랜치를 Push해서 새 브랜치를 생성하고, 기존 브랜치를 삭제하는 방법으로 변경해야 합니다.

  1. Local branch 이름 변경하는 2 Step 그대로 진행
  2. 해당 브랜치 push
$ git push origin -u 원격에새로만들브랜치명

$ git push origin -u new-branch  
$ --> 원격에도 new-branch라는 신규 브랜치가 생성됨

이 경우에도, old-branch의 commit/push 내역을 그대로 유지한 채 신규 branch가 생성된다!

  1. 기존 브랜치 삭제
$ git push origin --delete 삭제할이전원격브랜치명

$ git push origin --delete old-branch

필요 없어진 이전 브랜치를 삭제하면 된다!