Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename a local Git branch?

How do I rename a local branch which has not yet been pushed to a remote repository?

Related:

  • Rename master branch for both local and remote Git repositories
  • How do I rename both a Git local and remote branch name?
like image 738
Forrest Avatar asked Jul 06 '11 03:07

Forrest


People also ask

Can you rename a local git branch?

The steps to change a git branch name are: Rename the Git branch locally with the git branch -m new-branch-name command. Push the new branch to your GitHub or GitLab repo. Delete the branch with the old name from your remote repo.

Can we rename branch name?

Here are the steps to rename the branch: Switch to the branch which needs to be renamed. git branch -m <new_name> git push origin :<old_name>


4 Answers

To rename the current branch:

git branch -m <newname>

To rename a branch while pointed to any branch:

git branch -m <oldname> <newname>

-m is short for --move.


To push the local branch and reset the upstream branch:

git push origin -u <newname>

To delete the remote branch:

git push origin --delete <oldname>

To create a git rename alias:

git config --global alias.rename 'branch -m'

On Windows or another case-insensitive filesystem, use -M if there are only capitalization changes in the name. Otherwise, Git will throw a "branch already exists" error.

git branch -M <newname>
like image 89
siride Avatar answered Oct 14 '22 03:10

siride


git branch -m old_branch_name new_branch_name

The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any.

If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):

git push origin new_branch_name:master (now changes will go to master branch but your local branch name is new_branch_name)

For more details, see "How to rename your local branch name in Git."

like image 45
Madhan Ayyasamy Avatar answered Oct 14 '22 03:10

Madhan Ayyasamy


To rename your current branch:

git branch -m <newname>
like image 437
Jonathan Avatar answered Oct 14 '22 03:10

Jonathan


Here are the steps to rename the branch:

  1. Switch to the branch which needs to be renamed
  2. git branch -m <new_name>
  3. git push origin :<old_name>
  4. git push origin <new_name>:refs/heads/<new_name>

EDIT (12/01/2017): Make sure you run command git status and check that the newly created branch is pointing to its own ref and not the older one. If you find the reference to the older branch, you need to unset the upstream using:

git branch --unset-upstream
like image 380
Milind Anantwar Avatar answered Oct 14 '22 03:10

Milind Anantwar