Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename local branch not pused in remote [duplicate]

How do I rename a local branch which hasn't 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 989
Forrest Avatar asked Jul 06 '11 03:07

Forrest


7 Answers

To rename a branch while pointed to any branch:

git branch -m <oldname> <newname>

To rename the current branch:

git branch -m <newname>

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>

A way to remember this is -m is for "move" (or mv), which is how you rename files. Adding an alias could also help. To do so, run the following:

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

If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M, otherwise, git will throw branch already exists error:

git branch -M <newname>
like image 117
siride Avatar answered Oct 03 '22 18: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 32
Madhan Ayyasamy Avatar answered Oct 03 '22 18:10

Madhan Ayyasamy


To rename your current branch:

git branch -m <newname>
like image 45
Jonathan Avatar answered Oct 03 '22 18: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 29
Milind Anantwar Avatar answered Oct 03 '22 19:10

Milind Anantwar


Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one.

From my experience, to rename a local and remote branch in Git you should do the following steps.

Quoting from Multiple States - Rename a local and remote branch in git

1. Rename your local branch

If you are on the branch you want to rename:

git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch

git push origin -u new-name
like image 20
trungk18 Avatar answered Oct 03 '22 18:10

trungk18


The answers so far have been correct, but here is some additional information:

One can safely rename a branch with '-m' (move), but one has to be careful with '-M', because it forces the rename, even if there is an existing branch with the same name already. Here is the excerpt from the 'git-branch' man page:

With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.

like image 45
Vanchev Avatar answered Oct 03 '22 18:10

Vanchev


1. Rename

If it is your current branch, just do

git branch -m new_name

If it is another branch you want to rename

git branch -m old_name new_name

2. Track a new remote branch

- If your branch was pushed, then after renaming you need to delete it from the remote Git repository and ask your new local to track a new remote branch:

git push origin :old_name
git push --set-upstream origin new_name
like image 34
Oss Avatar answered Oct 03 '22 18:10

Oss