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.