Rename a Git branch
Published:Intro
Git is the de facto version control tool. While it is a powerful tool, it can be quite confusing. This is true for seemingly simple actions such as renaming a branch. We are going to demonstrate how to do so, so let’s get started.
Rename an existing branch
For this example we will start with a test repository called lively-git
.
The repository’s initial branch will be called master
.
Let’s say we want to rename our branch to main
because we would like our language to be more inclusive.
Before we rename, let’s double-check our local status.
git status
This command shows us the status of our local repository. We haven’t done anything other than initializing an empty repository, so we see very little details.
doug@Dougs-MBP dougy-demos % git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
We can see from the status message that we are on the master
branch,
and it also provides some additional detail that we will be ignoring for this post.
We will now run the following command to rename it to main.
git branch -m main
Above we see the output of renaming the local master
branch.
While we’ve made our change locally, we need to push it to our origin.
git push -u origin main
The above command has pushed our renamed branch to the origin repository.
Depending on your git platform, you most likely have to make some changes before deleting the old master
branch.
Git will throw an error is master
is protected, so read error messages carefully.
We will not cover that here; it is dependent on the user’s Git platform and its settings.
git push —delete origin master
We see some information after the command finishes executing. Let’s take a look:
doug@Dougs-MBP dougy-demos % git push --delete origin master
To github.com:demodoug/dougy-demos.git
- [deleted] master
From the output, we can see that we have successfully deleted the remote master
branch.
Our repository’s primary branch is now named main
.
Much better!
Conclusion
Git is a powerful tool with countless options and capabilities. We’ve covered how to perform a simple branch modification, but I urge you to continue to explore all it has to offer.