How to With Git: Rename Branch

How to With Git: Rename Branch

A good Git workflow should make use of branches for feature development, so you don’t clutter up your master branch with every change you make to your code. Learning how to rename a branch in Git can help keep your project history clean and easy to understand when it comes to figuring out when a new feature was introduced into your code.

In this guide, we’ll cover how to rename a local Git branch, as well as how to rename a remote Git branch, and even rename a remote Github branch.

These all will help keep your development flow nice, clean, and easy to manage down the road when coming back to a project.

Rename a Local Git Branch

To rename a local Git branch, we can use the Git branch -m command to modify the name:

That was easy, right?

Rename a Remote Git Branch

If you’re just looking for the command to rename a remote Git branch, this is it:

Full Example to Rename a Remote GitHub Branch

To explain renaming a remote Git branch, we’ll use a fictional GitHub project to show the process.

Clone a GitHub Repo

First, we clone the GitHub project to our local box with the Git clone command:

Now our myproject repository was cloned from GitHub.

The Git remote command shows all tracked repositories we can pull from and push to:

In this case, we just see the default Git origin name for a remote server.

We can look more verbosely at this with the Git remote -v command and flag:

Push Local Git Branch to GitHub Repo

Now that you have a GitHub project downloaded locally, you can create a local branch:

This is shorthand for:

After making changes, push “feature2to the remote repository named “origin”:

Checkout a Remote Git Branch From a GitHub Repo

If the “feature2” branch just existed remotely on GitHub, you won’t be able to push it.

You can use the Git branch -a command to see all available local and remote branches:

You need to first check out the remote repository branch, give it a local name, and switch to it:

Rename Git Branch on Remote GitHub Repo

Now we can rename the remote branch to “feature3” and push it to the remote origin:

We now linked our local “feature2” branch to the remote “feature3” branch.

Any changes in the “feature2” local branch will be pushed to “feature3.”

To fix this, we should also do a local Git branch rename like we did before:

Delete Git Branch on Remote GitHub Repo

After renaming a remote branch, the old branch remains as well.

You can simply delete the old branch if you no longer need it:

Tracking Issues With Git and Troubleshooting Them

Tracking issues arise from a lost “tracking reference” with a remote “repository:branch”.

These issues are indicated by Git using messages like the ones below when you try to push or pull code:

This could happen if the remote branch was renamed to “feature4,” but the local wasn’t updated.

You can use the Git branch -a command to help see how to fix local vs remote branch issues.

Then you can use Git checkout and Git branch -u to correct the relationship.

Finally, rename the local branch to match the remote branch for simpler future management:

Conclusion

Hopefully you are now comfortable renaming Git branches either locally or remotely. Mastering the ability to use the Git branch rename functionality can save you a lot of development headaches down the road.

Questions or Comments? Ask Jacob!

Ask a question and Jacob will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.