How to Rename a Branch in Git

Git is a popular version control system that makes it super easy to change branch names. Developers may wish to rename branch names for many reasons. So open-source collaborators should also know how to rename a branch in Git.

To make things simple for you, we will explain how to rename both local and remote git branches.

How to Rename a Local Branch in Git

Git users typically work in local versions of their projects. Once a change is validated, it is integrated to the upstream. It’s always a good idea to see which local branches are available before you rename one of them.

$ git branch
$ git branch -a

You can find out the local branches for your Git project using the above commands. The -a option also lists the remote branches. Now, you can proceed with renaming your local Git branch. Follow the below steps for doing this from your terminal.

1. Verify the Local Branch

$ git checkout 
$ git checkout alpha

The git checkout command allows us to switch between branches and verify working trees. If you’re on the alpha branch, the last command will confirm it. If you’re on a different branch, it will switch to alpha.

2. Rename the Local Branch 

Once you switch to the desired branch, you can rename it using the git rename branch command.

$ git branch -m 
$ git branch -m beta

This command changes the name of the local branch alpha to beta.

You can also rename a local branch from inside another git branch. Use the following command to do this from your favorite Linux terminal.

$ git branch -m  
$ git branch -m alpha beta

3. Verify the New Branch Name

You can easily verify whether the git rename branch operation was successful by listing the branches once more.

$ git branch -a

How to Rename a Remote Branch in Git

Git refers to remote versions of your repositories as simply ‘remotes’. You can not rename a remote branch as easily as local branches. Instead, you will need first to rename the local branch, push the new branch to the server, and delete the old branch from your repository.

1. Rename the Local Branch

Rename the local branch alpha to beta using the git rename branch command below.

$ git branch -m beta

or

$ git branch -m alpha beta

2. Push the Updated Branch

Push the renamed branch beta to the remote server using the following command.

$ git push origin 
$ git push origin beta

3. Set the Upstream

You need to set the upstream to make sure Git can track changes between your remote and local branches.

$ git push origin -u 
$ git push origin -u beta

Git will set up tracking between the local branch beta and the remote branch beta.

4. Remove the Old Branch

You can safely delete the old branch from your remote once you’ve pushed the renamed branch and set up the upstream. Use the following git command for deleting a remote branch in Git.

$ git push origin --delete 
$ git push origin --delete alpha

Git will proceed to delete the alpha branch from the remote origin.

5. Verify the Remote Branch

List the remote git branches once more to verify if the branch renaming operation was successful or not. You should see the new remote branch beta after issuing the following command.

$ git branch -a

Rename Git Branches Effectively

Renaming git branches is quite straightforward once you learn some basic git operations. You can easily change branch names for local branches. We’ve also covered the necessary git rename branch commands for remote branches. Try some of the commands on your test projects to get the relevant experience.

Source: makeuseof.com

Related posts

Connections #337: Today’s Answer and Clues (Monday, May 13, 2024)

The 5 Key Differences Between the Google Pixel 8a and Pixel 8

Putting 7 Free Adobe Apps to the Test: Do They Measure Up to the Paid Versions?