Blog Post
How to Quickly Rename a Local and Remote Branch in Git
Tutorials

How to Quickly Rename a Local and Remote Branch in Git

Correctly naming your Git branches is always important, especially when you’re working alongside other people on a project. Ignoring proper naming conventions can lead to a number of issues, not just for yourself, but also for everybody else that’s involved. Aside from potentially setting back your co-workers, you’ll likely receive more than a few complaints about failing to correctly name your branches. Needless to say, this is a situation you’ll want to avoid.

Even veterans can sometimes incorrectly name a Git branch and push it to the remote repository before realizing the mistake. Since you’re reading this article, chances are that’s already happened to you and you’re well aware of the consequences. However, there’s no need to worry if you messed up because Git allows users to rename branches even after they’ve been pushed to the remote repository. All you need to fix the problem are a few simple commands, which we’re going to discuss down below.

How to Rename a Local and Remote Branch in Git

Your first order of business is to switch to the local branch that needs to be renamed if you haven’t done so already. This can be achieved by typing in the following command into the terminal:

git checkout <old_name>

Next, you’ll want to rename the local Git branch by using the command:

git branch -m <new_name>

If you’re on a different branch and don’t want to switch to the local one for some reason, you can use the command below instead:

git branch -m old-name new-name

If the branch has already been pushed to the remote repository you need to delete it by using the following command:

git push origin --delete <old_name>

Last but not least, you’ll need to push the newly named local branch to the remote repository. This can be achieved by typing in:

git push origin -u <new_name>

That’s pretty much all there is to it. Once you’ve performed the sequence of commands listed above, both the local and remote branches in Git will be renamed and you’ll be all good to go.

Final Thoughts

Failing to follow naming conventions in Git can lead to various problems, but as you can see, it’s definitely not the end of the world if you’ve made a mistake. Although you can’t rename remote Git branches directly, you can use the commands above to rename a local one and push it to the remote repository. Just remember to delete the old remote branch first before pushing in the new one.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2020 Blackdown.org. All rights reserved.