

Messy branch names? Rename them in Git and keep your code organized with the help of this guide 👇 Click to Tweet Summary If the reference is pointing at the old branch, you can use git branch -unset-upstream to fix it. …then push the new one: git push origin -u new-branchĪs an alternative, you can overwrite the upstream branch name using the following: git push origin: old-branch new-branch git push origin -u new-branchĪgain, run git status to see if the new branch points to its own reference, rather than the older one. The first step is to rename the local branch, but from there you can run a couple of commands in turn.įirst, delete the old branch: git push origin -delete old-branch The trick is to remove the old branch from the remote repo, then push the new branch upstream. If you skip this part of the process, the new branch will still reference its old “upstream” branch name. You’ll more often than not have to handle remote repos within your project, and this step is one you shouldn’t forget. While this method works for a local repo, there’s more you need to do when it comes to remote ones. Pushing the Renamed Branch to the Remote Repo The command to change the branch name is similar to the last method, but this time you’ll combine both the current and new branch names: git branch -m current-name new-nameĪfter you run the command, you can check the list of branches as confirmation. Regardless, you can see your current location in the repo using git status. If not, you’ll always need to use a git checkout to get to your main branch. This method will only work if you are in the main branch of your repo. Renaming a Git Branch Without Using git checkout Let’s quickly look at this in more detail. However, you don’t need to use git checkout at all if you don’t want to. Once you confirm this change, use git status to see whether the new name applies to your branch: Checking that a Git rename has happened using git status.
Renaming git branch windows#
This is also the tactic you’ll use on Windows machines if your branch name contains a mixture of upper- and lower-case lettering. If the new branch name you select already exists, you can force the change you specify using -M instead. The -m flag means “move” and is the element of the command that causes the rename to happen. Once you’re in the right branch, you can rename it using the following: git branch -m new-nameĪgain, you’ll want to make sure you use a specific branch name here, rather than our placeholder. Navigating to, and listing, the branches within a Git repo. If you don’t know the name, or want to see the complete list of branches (maybe to check for conventions across the project), you can use git branch -list or git branch -a. Where we use current-name, you should add the name of your branch. You can use the following command: git checkout current-name Your first task is to locate the branch you want to rename. Use the git checkout and git branch Commands
Renaming git branch how to#
Then, we’ll look at how to make sure your remote repo matches the local one. This will include looking at the command flags that will help you. The good news is that the process we’re about to outline will cover all of the permutations you’ll need to succeed.įirst, let’s cover how to locate and change the name of a branch. When it comes to running a git rename on a branch, you’ll have to consider both local and remote (e.g. 🤝 Click to Tweet How To Rename a Git Branch Learn how to git rename a branch and keep your team on the same page with this guide.

Don't let local and remote repos trip you up. Next, we’ll walk you through how to do it. As an example, there is a popular movement to change the primary branch from “master” to “main” or “trunk” to avoid references to the institution of slavery.īecause of these reasons (and others), you’ll often have to perform a git rename on a branch.

