remote - rename branch github




How do I rename a local Git branch? (20)

I don't want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.

How can I rename a local branch which hasn't been pushed to a remote branch?


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

  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
  1. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

  1. Reset the upstream branch for the new-name local branch. Switch to the branch and then:

git push origin -u new-name

Or for a fast way to do that, you can use these 3 steps:

# Rename branch locally

git branch -m old_branch new_branch  

# Delete the old remote branch

git push origin :old_branch  

# Push the new branch, set local branch to track the new remote

git push --set-upstream origin new_branch   

Referance: https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html


git version 2.9.2

If you want to change the name of the local branch you are on:

git branch -m new_name

If you want to change the name of a different branch:

git branch -m old_name new_name

If you want to change the name of a different branch to a name that already exists:

git branch -M old_name new_name_that_already_exists

Note: The last command is destructive and will rename your branch, but you will lose the old branch with that name and those commits because branch names must be unique.


Advanced Git users can rename manually:

Rename the old branch under .git/refs/heads to the new name

Rename the old branch under .git/logs/refs/heads to the new name

Update the .git/HEAD to point to yout new branch name

Another option is not to use the command line at all. Git GUI clients such as SourceTree take away much of the syntactical learning curve / pain that causes questions such as this one to be amongst the most viewed on .

In SourceTree, right click on any local branch in the "Branches" pane on the left and select "Rename ...".


Changing the branch locally is quite easy...

If you are on the branch you want to change the name, simply do this:

git branch -m my_new_branch

Otherwise, if you are on master or any other branch other than the one you'd like to change the name, simply do:

git branch -m my_old_branch my_new_branch

Also, I create the image below to show this in action in a command line, in this case, you are on master branch for example:


Here are the steps to rename the branch:

1. switch to 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 newly created branch is pointing to its own ref and not older one. If you find the reference to older branch, you need to unset the upstream using:

git branch --unset-upstream

Here are three steps: A command that you can call inside your terminal and change branch name.

git branch -m old_branch new_branch         # Rename branch locally
git push origin :old_branch                 # Delete the old branch
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

If you need more: step-by-step, How To Change Git Branch Name is a good article about that.


If you are willing to use SourceTree (which I strongly recommend), you can right click your branch and chose 'Rename'.


If you want to change the name of the current branch, run:

git branch -m [old_branch] [new_branch]

If you want to delete the old remote branch, run:

git push origin :[old_branch]

If you want to delete the old remote branch and create a new remote branch, run:

git push origin :old_branch new_branch

If you want to:

  • Rename the git repository, run: git branch -m <oldname> <newname>
  • Delete old branch by: git push origin: old-name new-name
  • Commit it using: git commit <newname>
    • and then push using: git push origin new_branch_name:master
  • If you want to check status then use: git status
  • If you want to check out then use: git checkout

PHPStorm:

VCS->Git->Branches...->Local Branches->_your_branch_->Rename


Rename the branch using this command:

git branch -m [old_branch_name] [new_branch_name]

-m: It renames/moves the branch. If there is already a branch, you will get an error.

If there is already a branch and you want to rename with that branch, use:

 git rename -M [old_branch_name] [new_branch_name]

For more information about help, use this command in the terminal:

git branch --help

or

man git branch

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

Since you do not want to push the branch to a remote server, this example will be useful:

Let's say you have an existing branch called "my-hot-feature," and you want to rename it to "feature-15."

First, you want to change your local branch. This couldn't be easier:

git branch -m my-hot-feature feature-15

For more information, you can visit Locally and Remotely Renaming a Branch in Git.


The answers so far have been correct but here is some additional info: One can rename a branch with '-m' (move), but one has to be careful, because '-M' 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.


To rename current branch (except for detached HEAD state) you can also use this alias:

[alias]
    mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'

To rename your current branch:

git branch -m <newname>

git branch rename can be done by using

  1. git branch -m oldBranch newBranch

  2. git branch -M oldBranch ExistingBranch

Difference between -m and -M ,

-m: if your trying to rename your branch with exising branch name using -m it will raise an error says, branch already exist. you need to give unique name.

but,

-M: this will help you to force rename with given name even it is exist. so existing branch will overwrite entirely with it...

Here is git terminal example,

[email protected]:~/project/myapp/sunithamakeup$ git branch 
  master
  master0
  new_master
  test
* test1
[email protected]:~/project/myapp/sunithamakeup$ git branch -m test1 test
fatal: A branch named 'test' already exists.
[email protected]:~/project/myapp/sunithamakeup$ git branch -M test1 test
[email protected]:~/project/myapp/sunithamakeup$ git branch 
  master
  master0
  new_master
* test
[email protected]:~/project/myapp/sunithamakeup$ 

git branch -m old_branch_name  new_branch_name

or

git branch --move old_branch_name new_branch_name






git-branch