Git & GitHub Interview Question #27

How do you rename a Git branch?

Remotes, Fetch, Pull, Push, Merge, Conflicts, Rebase & Stash Junior Beginner

Quick Interview Answer

A local branch can be renamed with git branch -m. If the renamed branch has already been published, the remote branch and upstream configuration may also need to be updated.

Detailed Explanation

A local branch can be renamed with git branch -m. If the renamed branch has already been published, the remote branch and upstream configuration may also need to be updated.

Renaming a local branch does not automatically rename the corresponding branch on a remote server.

Code Example

# Rename the current branch
git branch -m new-name

# Rename another local branch
git branch -m old-name new-name