While working with branches in Git, users often want to rename them due to some reasons, such as duplicate branch names, change for clarity, or a particular branch that is no longer accurate. In such situations, Git provides the “git branch -m <new-branch-name>” command that allows developers to rename it.
In this guide, we will talk about renaming GitLab branches using the Git utility.
How to Reset a GitLab Branch Name Using Git?
To rename a branch in GitLab, check out the provided steps:
- Redirect to the Git root directory.
- List all the existing branches.
- Switch the branch and run the “git branch -m <branch-name>” command.
- Update the GitLab remote host using the “git push <remote-name> -u <branch-name>” command.
- Execute the “git push <remote-name> –delete <branch-name>” command to delete the existing branch remotely.
Step 1: Move to Git Root Directory
Initially, run the “cd” command with the Git root directory path and move to it:
Step 2: List All Branches
Then, list all the branches including remote as well as local by running the “git branch -a” command:
From the given output, we have selected the “dev” local branch for further process:
Step 3: Switch to Local Branch
Now, execute the “git checkout” command along with the branch name and switch to it:
Step 4: Rename the Branch Locally
Next, rename the current working branch by using the “git branch -m” command along with the new branch name:
Step 5: View All Branches
To check if the branch name has been changed or not, run the following command:
As you can see, the old “dev” branch has been renamed as “beta” successfully:
Step 6: Push Renamed Local Branch to Remote
After renaming the branch names locally, users need to update the remote repository as well. To do so, run the “git push” command with the remote name “origin”, “-u” option for setting it as a tracking branch and the new branch name as ”beta”:
Step 7: Delete the Old Branch Remotely
To delete the renamed branch from the remote repository, execute the following command:
According to the below-given output, the branch has been deleted successfully:
That’s it! We have described renaming a GitLab branch using Git utility.
Conclusion
To rename a branch in GitLab, first, redirect to the Git root directory and display all branches. Then, select one of them and switch to it. Next, use the “git branch -m <branch-name>” command. After that, update the remote repository by running the “git push <remote-name> -u <branch-name>” command. Lastly, execute the “git push <remote-name> –delete <branch-name>” command to delete the existing branch remotely. In this tutorial, we have demonstrated renaming a GitLab branch using Git.