My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Saturday, February 28, 2026

How To change a Git branch name

To change a Git branch name, you first rename the branch locally using git branch -m and then update the remote repository.

Renaming a Local Branch
You can rename a local branch in one of two ways:
  • If you are on the branch you want to rename:
    bash
    git branch -m <new-name>
    
    For example: git branch -m feature/new-login.
  • If you are on a different branch:
    bash
    git branch -m <old-name> <new-name>
    
    For example: git branch -m feature/old-login feature/new-login.
Renaming a Remote Branch
Renaming a branch that has already been pushed to a remote repository (like GitHub or GitLab) requires a few extra steps and team coordination.
  1. Rename your local branch using one of the methods above.
  2. Push the newly named local branch to the remote repository. This effectively creates a new branch on the remote.
    bash
    git push origin -u <new-name>
    
    The -u (or --set-upstream) flag is important as it links your local branch to the new remote branch.
  3. Delete the old branch from the remote repository to avoid confusion:
    bash
    git push origin --delete <old-name>
    
  4. Update settings on your Git host (e.g., GitHub, GitLab, Bitbucket) if the renamed branch was the default branch or if there were branch protection rules, CI/CD pipelines, or open pull requests associated with the old name.

No comments:

Post a Comment