To rename a local branch from inside another, use “git branch -m old-name new-name.”
Branching is a trivial exercise in Git.
Unfortunately, trivial tasks rarely get due attention, and mistakes happen.
If you’ve misnamed a branch, it’ll have to be renamed.
Here’s how to do that.
Operations involving branches were often very slow, too.
There was a time penalty top using branches.
Git was designed from the get-go to be different.
Branching in Git is very cheap in computational terms.
It is a trivial action inside the local repository.
In fact, developers are encouraged to branch, and to branch often.
Branches are just another tool inside your VCS that you benefit from.
Branches aren’t big scary things in Git, they’re one of its most used features.
But familiarity can lead to contempt.
Or, at least, to a casual regard for branches.
If it isn’t, it’ll cause confusion when others give a shot to use it.
Thegit branchcommand lists branches for us.
The current branch is highlighted in green, and with an asterisk.
To see the branches and theircommits, you’re able to use theshow-branchcommand.
To see local and remote branches with one command, use the-a(all) option.
We have more local branches than we have remote branches.
Branch “feature16” hasn’t been pushed to the remote repository yet.
That’s just an aspect of normal operation, not a problem.
Our problem is branch “feature19” should have been named “feature18.”
So that’s the mistake we’re going to correct.
Then use thegit branchcommand with the-m(move) option.
We checked out the “feature19” branch and renamed it to “feature18.”
Let’s see what our branches look like now.
Our branch now has the correct name in the local repository.
If you want to, you might rename a branch when you’re working in another branch.
Here’s an example where we’re working in the “master” branch.
Again, the branch in our local repository has been renamed with the correct name.
To rename a remote branch, we delete the old branch and push the new branch.
Let’s take a look at the state of our branches.
We’ll use the-a(all) option to see both the local and the remote branches.
You’ll be prompted for the password for the remote repository.
Once the deletion has taken place you’ll see a confirmation message.
Let’s check once more what state our local and remote branches are in.
Renaming Branches Is Simple
With Git, renaming branches is simple.
When they’ve pushed, you could pull the branch, then rename your branch locally and remotely.