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.

Topics in this guide

Rename a Git branch using VSC

  1. Open VSC, and choose File > Open folder (top menu).

    'Open folder' top menu option in VSC

  2. Use the VSC open folder explorer to select the local repo folder on your computer, then choose Select folder.

    'Select folder' option in the 'VSC open folder explorer'

  3. From the VSC top menu, choose Terminal > New Terminal.

    'New Terminal' top menu option in VSC

    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.

  4. 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
    

    'Git branch rename' command in the 'VSC Terminal'

    The branch name in the VSC status bar should now change to the new branch name.

    The new branch name in the 'VSC status bar'

    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.

  5. 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
    

    'Git delete branch' command in the 'VSC Terminal'

    A message in the VSC Terminal confirms that the branch is deleted from GitHub/ AzDevOps.

    'Git delete branch' confirmation message the 'VSC Terminal'

  6. 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
    

    'Git push new branch name' command in the 'VSC Terminal'

    A message in the VSC Terminal confirms that the branch is renamed on GitHub/ AzDevOps.

    'Git push new branch name' confirmation message the 'VSC Terminal'

  7. 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
    

    'Git set upstream command' in the 'VSC Terminal'

    The Everything up-to-date message in the VSC Terminal confirms that the branch link is reset.

    'Everything up-to-date' confirmation message the 'VSC Terminal'

  8. 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.

    Current branch set to new branch name in the 'VSC status bar'

You’ve renamed a branch using VSC successfully.

Appendices

Check the following supplementary Appendices for more details and context.