Post

Git: Rename a Local and Remote Git Branch

간혹 Git을 사용하면서 branch 이름을 변경 해야 할 때가 있었다. 회사에서 프로젝트를 진행하며 branch의 이름을 바꾸는 경우는 드물었지만 개인 Project나 공부를 할 때, 각각의 branch로 Project의 타임라인을 포맷에 맞춰 이름을 바꾼 방법을 공유한다.

Git

Rename Local Git Branch

  1. 이름을 바꾸고자 하는 local branch로 들어간다.

    1
    
    $ git checkout <old_branch_name>
    
  2. branch에 새로운 이름을 부여한다.

    1
    
    $ git branch -m <new_branch_name>
    

Rename Remote Git Branch

작업하는 branchremote branchpush되지 않았다면, 위의 방법으로 branch이름을 바꾼뒤 작업을 push하였을 때 새로운 이름이 remote branch에 제대로 저장된 것을 볼 수 있다. 하지만 이미 pushbranch라면, 아래의 방법으로 remote branch의 이름을 바꿀 수 있다.

  1. 새로운 branchupstream을 이용해 reset한다

    1
    
    $ git push origin -u <new_branch_name>
    
  2. 바뀌기 전 branch를 지운다.

    1
    
    $ git push origin --delete <old_branch_name>
    

References:

This post is licensed under CC BY 4.0 by the author.