6

How to Rename a Branch in Git

 4 years ago
source link: https://www.makeuseof.com/git-rename-branch/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 <old-branch-name>
$ 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.

verifying git branch before changing branch name

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 <new-branch-name>
$ git branch -m beta

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

change local branch name using git rename branch command

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 <old-branch-name> <new-branch-name>
$ 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
verify renamed branch in git

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
$ 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 <new-branch-name>
$ git push origin beta
push updated branch to git

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 <new-branch-name>
$ git push origin -u beta

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

set upstream after git rename branch

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 <old-branch-name>
$ git push origin --delete alpha

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

delete old git branch

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
verify remote git branch after git rename branch

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.

About The Author

5fd57720021f4-pro-pic.jpg?fit=crop&w=100&h=100

Rubaiat Hossain (12 Articles Published)

Rubaiat is a CS grad with a strong passion for open-source. Apart from being a Unix veteran, he's also into network security, cryptography, and functional programming. He's an avid collector of secondhand books and has a never-ending admiration for classic rock.

More From Rubaiat Hossain

Subscribe To Our Newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK