Rename a Git branch locally and remotely?

Rémi Becheras :

Is there a way to rename a Git branch locally and push it to the remote branch, even if there are already many commits pushed to the remote branch?

Or, is it necessary to create a new local branch, delete the old local branch, and then repeat the operation on the remote repository?

Rémi Becheras :

Yes,

the feature move exists to rename the branch locally

git branch --move <old_name> <new_name>

but to push it, you must delete the old and push the new

git checkout <new_name>
git push origin [--set-upstream] <new_name>
git push origin --delete <old_name>

--set-upstream is optional, it configure the new local branch to track the pushed one

You can use the following shorthands:

  • move locally (--move) :

      git branch -m <old_name> <new_name>
    
  • push new branch (--set-upstream, optional) :

      git push origin [-u] <new_name>
    
  • delete (--delete) :

      git push origin -d <old_name>
    

NB.

Thanks to torek's comment:

Worth mentioning, by the way, is that you should

  1. notify other users who share the upstream that you will be doing this, and
  2. do this in the order shown (set new name, then delete old).

The reason for #1 is that those users will need to adjust.

The reason for #2 is mainly just efficiency: it avoids having to re-copy objects to an upstream repo that drops commits on branch deletion (most bare repositories do that, and most repositories that accept pushes are bare)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Will 'git push origin <branch>' remove <branch> remotely after removing it locally?

How do I delete a Git branch locally and remotely?

Rename a branch in git flow

Delete local "remote branch" after the branch was deleted locally and remotely

Working locally and remotely at the same time with git

Git: Merge a Remote branch locally

Git: How to merge branch to master locally and on Github?

How do I delete a Git branch locally?

Git: removing remote branch after removing it locally

Problems setting git up locally/remotely (with Coda 2 and Terminal)

git merge origin/branch vs. merge branch locally

How do I rename a local Git branch?

How can I rename a local Git branch?

Rename git branch name on remote using PyGithub

Mercurial equivalent of this git: rename branch and pull remote branch

Rename local GIT branch breaks Git Flow in SourceTree

Should I do git fetch before pull if branch exists locally?

Getting the git differences between a locally changed file and a different branch

Add git layer on SVN repository to be able to branch locally

How to refresh a file that was deleted locally from a remote git branch

How to see a remote branch, created with ADO, locally with a git command?

Rename master branch for both local and remote Git repositories

How do I rename both a Git local and remote branch name?

Rename multiple names and emails in a single pass of git-filter-branch

How do I rename my Git 'master' branch to 'release'?

Rename all Git branch names to lowercase on Windows (GitHub remote)

git 2018: Is there a way to rename a branch at remote for all users?

Git - Rename a local branch which has 2 commits pushed to remote

How do I prevent git from trying to push files I have deleted locally and remotely?