Hello, friends. This post is quite short, but it can help more than one novice programmer. Today, you will learn how to rename a GIT branch. Let’s go for it.
When we as programmers work with GIT, there can always be an innocent mistake when creating development branches. Who did I pass it on to? Fortunately, it’s a mistake that has a simple fix.
Also read: How to install Git on Ubuntu 22.04?
In this post, you will learn how to do it both remotely and locally so that you can fix it in any environment.
Renaming a GIT branch
Suppose you created a branch in GIT. Most likely you ran something like this.
git checkout -b [name]
But for some reason, you have to modify the branch name. Well, it’s easy. Let’s look at both cases, whether it’s local or remote.
Renaming a GIT branch (Local)
To rename a GIT branch locally, you need to run the
checkout command like this
git checkout -m [new-name]
The above command will work only if you are located in the branch to rename. Otherwise, you will have to run something like this
git branch -m [old-name] [new-name]
This way, you will be able to rename the development branch locally. As you can see, it’s nothing complex.
Renaming a GIT branch remotely
If you have already pushed from the repository to the already created branch, and it is in
origin you will have to do a push to reflect the changes in the repository
git push origin -u [new-name]
And now you can delete the old one because the other one has already been uploaded.
git push origin --delete [old-name-branch]
This way you can make the change and, as you can see, it is simple.
GIT is a basic tool for programmers and although it is easy to use, we can always find a trick or two in its use.