Introduction to git change branch name
Git uses branches to facilitate work progress by multiple programmers on
a collaborated project. Git change branch name commands like
git branch and git checkout are used by developers to correct wrong
branch names in git.Upon assignment of responsibilities in git, each
developer can independently make changes to codes without being held
back by others.
Depending on the rising needs, you can choose to change a branch name or create a new branch for modifying your codes to serve the purpose. Since branches are an essential part of the git work division, knowing how to name and change a branch name is crucial.
git branch command is what you will use for creating branches and to
change the name of a branch.
In this tutorial, we shall discuss steps that you can use to change a branch name in git.
Git change branch name workflow
Here we have a master branch from where we checked out (switched) to my-feature-branch. Later we changed the branch name from my-feature-branch to new-feature-branch:
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/git_change_branch_name.webp)
Git change branch name syntax
We can use the following syntax to change the branch name:
git branch -m <oldname> <newname>
OR if you are already inside your old branch which you intend to rename then you can use:
git branch -m <newname>
Setting up the lab environment
It’s vital that you set up a lab environment that you can use to
practice your codes. For this tutorial, we will clone a remote project
change-b-name to our local workstation. We shall then practice using
it to run the git change branch name exercise. I will be working with
windows 10 pro and git version 2.32.0.windows.2.
Below is the output after running the git clone command:
$ git clone https://github.com/J-Mulombi/change-b-name.git
Sample Output:
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/gitlab-75.webp)
Steps to git change branch name properly
Git employs the git branch –m <branch name > command to change the
name of a branch. –m option allows you to edit the new name and pass
it as the correct branch name. Below are the basic steps you can follow
to change a branch name:
Step-1: Switch to the branch that you want to change the name in git
It is important to ensure that you are on the branch that you wish to
change the name. You can use the git checkout <branch name> command
to confirm or switch to the desired branch for the name change process.
We shall demonstrate how to switch to a branch another using the example below:
First, let’s create a new branch text-branch in the local repository
change-b-name.
$ git branch text-branch
Next, we will run the git branch –a command to view all the branches
in our local repository as follows;
$ git branch -a
* master
text-branch
remotes/origin/HEAD -> origin/master
remotes/origin/master
From the output, we now have the new branch text-branch displayed
among the active repository’s branches.
Lastly, we shall then push the changes upstream as shown in the sample output below:
$ git push --set-upstream origin text-branch
Sample Output:
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/gitlab-76.webp)
While at this point, we realize that we have named the branch wrongfully and we need to change the branch name. How do you accomplish that?
First, let’s ensure that we are on the text-branch before proceeding
to the next step by running the git checkout <text-branch> command:
$ git checkout text-branch
Switched to branch 'text-branch'
Step-2: Change local branch name
To change the above branch text-branch to the correct name feature
branch we shall run the git branch –m <branch> command as illustrated
below:
$ git branch -m "feature"
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
From the results, we have successfully changed the local text-branch
name to feature.Note at this stage that we only changed the name of
the local branch, and that doesn’t affect the name change remotely.
Step-3: Change the remote branch name in git
Now that we have changed the branch name locally, we should also push the same change to remote repository.
To proceed to change the feature name remotely, we shall run the
git push origin :< old-branch-name> <new-branch-name> command as
follows:
$ git push origin :"text-branch" "feature"
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/J-Mulombi/change-b-name.git
- [deleted] text-branch
* [new branch] feature -> feature
Now, we also have the remote branch name test-branch changed to
feature as shown in the output. Note that the above command will
delete the text-branch from the remote repository.
text-branch and you push the
feature branch upstream like we just did, you will run into an error.
Understand also that at this point only the branch reference for the new name was changed in the remote, and not the changes committed in the branch.
Step-4: Update local changes/commits in the remote branch
You will need to run another step to push related changes/commits in the
feature branch to the remote repository change-b-name as follows:
$ git push origin -u feature
Sample Output:
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/gitlab-77.webp)
We successfully pushed the committed changes upstream. Now both the local and remote branches are up to date.
Let’s run the git status command to confirm:
$ git status
On branch feature
Your branch is up to date with 'origin/feature'.
nothing to commit, working tree clean
How to change the local branch name from “master” to “main” in git
To change the local master branch name to main we shall run the
following process:
Using the local repository change-b-name, we will first,
checkout master branch and then run the git branch -m <master main>
command.
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -m master main
$ git status
On branch main
Your branch is up to date with 'origin/master'.
Notice the git status confirms the name change from master to
main.Next, we shall proceed to make similar changes in the remote
repository.
Before proceeding, understand that git will not allow pushing the name
change upstream without first creating a remote branch named
main.Assuming that we are still on the main branch, we will now
push to remote the main branch as shown below:
$ git push -u origin main
Sample Output:
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/gitlab-78.webp)
The above results confirm that we have a new remote branch main.
Next, we will delete the origin master branch to complete the process by
running the git push origin --delete master command.
$ git push origin --delete master
To https://github.com/J-Mulombi/change-b-name.git
! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://github.com/Maureen-Mulombi/change-b-name.git'
We have run into an error!
The success of this step depends on the default set-up for the branch
settings in your Github account. You will therefore require to change
the default branch settings to main as shown in the screenshot below
before running the git push command again.
However, if your account has the default branch settings set at main, you will not experience such an error.
![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/git-default-branch-setup-e1630068614776.webp)
Now, we shall run the git push origin --delete master command.
$ git push origin --delete master
To https://github.com/Maureen-Mulombi/change-b-name.git
- [deleted] master
It’s done! The master branch is successfully deleted.
How to erase a local branch from a local repository
To delete a local branch in git, we shall use the
git branch –d <branch name> command as demonstrated below:
In this example, we shall delete the local feature branch.
$ git branch -d feature
warning: deleting branch 'feature' that has been merged to
'refs/remotes/origin/feature', but not yet merged to HEAD.
Deleted branch feature (was 60b7918).
We have now deleted the local branch feature as displayed in the
output.
Remember that for you to run the process without errors, you should have
the feature branch upstream. Alternatively, you can apply the
git branch -D branch name command to forcefully delete the local
branch even when there’s no upstream copy. That way you will avoid
running into an error.
How to erase a remote branch from a git repository
To delete a remote branch in git, you will require to specify the remote branch name in the delete command as shown below:
$ git push origin --delete <branch name>
In this example, we shall delete the remote tester branch in our
change-b-name remote repository as follows:
$ git push origin --delete tester
To https://github.com/J-Mulombi/change-b-name.git
- [deleted] tester
Summary
We have discussed the following topics about git change branch name:
- Introduction to git change branch name
- Steps for git change branch name
- How to change the local master name to main
- How to erase a local git branch
- How to remove a remote branch from a git repository

![git change branch name PROPERLY [3 Examples]](/git-change-branch-name-example/git_change_branch_name-1.jpg)
