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.

Listing local branches using the git branch command

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.

Listing branches and their commits with the git show-branch command

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.

Listing remote branches with the git branch -r command

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.

Listing local and remote branches with the git branch -a command

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.

Checking out branch “feature19” and renaming it

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.

Listing branches to check that branch “feature19” has been renamed to “feature18”

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.

Listing branches to check we’re on the master branch

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.”

Renaming one branch from inside another branch

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.”

Listing local and remote branches with the git branch -a command

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.

Deleting a remote branch using the git the –delete option

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.

Pushing a branch to the remote repository

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.

Listing local and remote branches with the git branch -a command

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.