How To Rename a Local and Remote Git Branch ?
j

Prakash Bhandari

November 24, 2020

In software development, developers use the Version Control Systems(VCS) to manage and organize their team work in a central repository. According to the latest Stack Overflow Developer Survey 2017, 69.2 percent of developers use Git, to define their workflow. Based on predefined workflow developers have to follow certain rules to create their new working branch. Sometimes, knowing or unknowingly developers fail to create new working branches with proper naming strategy. This is the most common and unavoidable mistake every developer will make. This article will describe how to correct those mistakes by renaming a local and remote Git branch without losing  their work.

Rename a Local Git branch 

Renaming a local git branch is easy and straightforward. We can run below commands to rename the local branches in git.  

1. Rename the branch that is currently checked out

git branch -m <new-branch-name>

2. Also, we can rename a local branch from another branch by using the following commands:

git checkout master

git branch -m <old-branch-name> <new-branch-name>

Image 1: Rename Git branch

Here, the -m option is an alias for --move

3. Verify the local branch has the correct name by checking the git  status :

 git branch –a

Image 2: Check Git local and remote branch

In image 2, we can see that the old_branch is renamed to new_branch. But, on remote we can still see the old branch name.

If <old-branch-name> branch has already been pushed to the remote repository,  then renaming the remote branch is not straightforward like a local branch. We need to perform additional steps.

Rename a remote Git Branch

We can not directly rename a Git branch in a remote repository. First, we need to delete the old branch and we can push a branch with the correct name to the remote repository. 

4. Delete the branch with the old name on the remote repository:

git push origin : <old-branch-name> <new-branch-name>

Image 3: Delete and Push new branch

5. Push the local branch with the correct name, and set the upstream branch: 

git push --set-upstream origin <new-branch-name>

Image 4: Track new branch

Finally, verify the local and remote branch has the correct name by checking the git  status.

git branch –a

Image 5: Check Git local and remote branches

In image 5,  we can see new branch name for both local and remote branches.

Now, we learned how to rename a local branch and a remote branch with the correct branch name.

Refrences:

  1. Git 2020, --distributed-is-the-new-centralized, Git, viewed 5 September 2020, https://git-scm.com/docs/git-branch.
  2. Stackoverflow 2017, Developer Survey Results 2017 ,  Stackoverflow , viewed 9 September 2020, https://insights.stackoverflow.com/survey/2017#technology.

Prakash is self motivated, hard working and personable IT professional with 9 years of experience in Software Development with cutting edge technologies. He has extensive background in full lifecycle of software development process including iterations of requirements gathering, prototyping, architecting, coding, peer review, refactoring, debugging, acceptance test and  release.

Prakash Bhandari

Software Engineer, BigTinCan - Sydney, Australia

Get connected with Prakash :

0 Comments

Related Articles