Rename branch
This guide describes how to rename a Git branch using the Visual Studio Code (VSC) editor.
Note: Take care when renaming a branch, and only rename a branch if you’re sure you need to.
Prerequisites
Before you proceed complete the following:
- Install Visual Studio Code
- Install Docs Authoring Pack
- Install Word Count extension
- Install Pull Requests extension
- Install Azure Repos extension
- Install Git client
- Set Git credentials
- Terminology and concepts
- General project workflow
- Download course files (clone repo)
- Project branching policies
- Create new branch in VSC
- Send (push) files
Topics in this guide
Rename a Git branch using VSC
-
Open VSC, and choose File > Open folder (top menu).
-
Use the VSC open folder explorer to select the local repo folder on your computer, then choose Select folder.
-
From the VSC top menu, choose Terminal > New Terminal.
Note: The VSC status bar, at the bottom of the editor window, indicates the name of the branch that VSC is currently switched to. In the previous image, the VSC status bar indicates that VSC is currently switched to the branch called old-name. The branch name in the VSC status bar will change to the new branch name after you’ve renamed the branch.
-
In the VSC Terminal, type the following command to rename your local branch, then press Enter.
For example, in the following sample command and image, the branch is renamed from old-name to new-name. Replace old-name and new-name with whatever branch names you require.
git branch -m old-name new-name
The branch name in the VSC status bar should now change to the new branch name.
Note: The Git branch command option
-m
is short for move, and the-m
command option is also used to rename a Git branch. Information about the Git move (rename) branch command, and other options, is available from the Software Freedom Conservancy’s Git branch documentation. -
Delete the old branch name from GitHub/ AzDevOps by typing the following command into the VSC Terminal, and then press Enter:
git push origin --delete old-name
A message in the VSC Terminal confirms that the branch is deleted from GitHub/ AzDevOps.
-
Push (send) the new branch name to GitHub/ AzDevOps by typing the following command into the VSC Terminal, and then press Enter.
git push origin new-name
A message in the VSC Terminal confirms that the branch is renamed on GitHub/ AzDevOps.
-
Reset the link between your renamed local branch and the new (upstream) branch you pushed to GitHub/ AzDevOps by typing the following command into the VSC Terminal, and then press Enter.
git push origin -u new-name
The Everything up-to-date message in the VSC Terminal confirms that the branch link is reset.
-
The VSC status bar should indicate that VSC is currently switched to your renamed branch. You can now make, save, stage, commit, and push file changes between the renamed local branch and GitHub/ AzDevOps.
You’ve renamed a branch using VSC successfully.
Appendices
Check the following supplementary Appendices for more details and context.