
Just remember to delete the old remote branch first before pushing in the new one. 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. Final Thoughtsįailing 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.
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. Verify the Local Branch git checkoutThis can be achieved by typing in: git push origin -u Last but not least, you’ll need to push the newly named local branch to the remote repository. 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 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 Next, you’ll want to rename the local Git branch by using the command: git branch -m Specified Old name of the branch is the name of the branch you need to rename, and new.
This can be achieved by typing in the following command into the terminal: git checkout To rename a branch, run git branch mYour first order of business is to switch to the local branch that needs to be renamed if you haven’t done so already.
#Renaming git branch how to#
How to Rename a Local and Remote Branch in Git All you need to fix the problem are a few simple commands, which we’re going to discuss down below. 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. Since you’re reading this article, chances are that’s already happened to you and you’re well aware of the consequences. Needless to say, this is a situation you’ll want to avoid.Įven veterans can sometimes incorrectly name a Git branch and push it to the remote repository before realizing the mistake.

Aside from potentially setting back your co-workers, you’ll likely receive more than a few complaints about failing to correctly name your branches. Ignoring proper naming conventions can lead to a number of issues, not just for yourself, but also for everybody else that’s involved. Once you understand what is going on, it isn't too bad, but it certainly requires more steps than simply renaming a local branch.Correctly naming your Git branches is always important, especially when you’re working alongside other people on a project. Reset the upstream reference for the renamed branch But slashes let you do some branch renaming when pushing or fetching to/from a remote.Delete the remote branch that is to be renamed.Rename the local branch using the same method shown the first section.In plain English, here is what is happening line-by-line: In terms of Git commands, here is how the process looks: $ git branch -m $ git push -d $ git push $ git checkout $ git push -u Rename the branch locally with the git branch -move command: git branch -move bad-branch-name corrected-branch-name This replaces your bad-branch-name with corrected-branch-name, but this change is only local for now. To do it, you'll need to rename the local branch, delete the remote branch, and then push the renamed local branch to the remote repo again. Renaming a remote branch is a bit more involved, and isn't actually possible in the same way it is for renaming local branches.

If you're wanting to rename the branch that is currently checked out, then you can omit the option, which looks like this: $ git branch -m Rename a Remote Git Branch Using the option in this way will let you rename the branch even if the new branch name already exists in your repository. The -m option is an alias for -move, which is analog to the Unix mv command.Ĭontinuing with the convention we saw with the delete branch option, capitalizing the flag as -M, which is an alias for -move -force, allows you to "force" the change. To rename a local branch, you'll want to use the branch command like this: $ git branch -m In this short article I'll show you how to rename Git branches for both local and remote repositories. And while it is also possible with remote branches, the process is a bit more involved with the use of a workaround, but still works as intended.

You can add an alias ' git new ' that will default to whatever starting branch you like.
#Renaming git branch update#
Did you make a mistake in naming your Git branch? Or maybe "branch-2" wasn't descriptive enough? Luckily in Git you can rename a local branch pretty easily. Update the default branch to be origin/main.
