Forrest
Forrest

Reputation: 127633

How can I rename a local Git branch?

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

Related:

Upvotes: 11849

Views: 5880209

Answers (30)

ahmnouira
ahmnouira

Reputation: 3391

You can do this, see the example below:

git branch -m QINS-38 QINS-38-old
git checkout -b QINS-38
git branch 

Upvotes: 5

abdoulsn
abdoulsn

Reputation: 1159

For more details on this procedure.

How to rename a local branch in Git

  • To rename the current branch, make sure you’ve checked out and are using the branch you want to rename.

    git checkout oldbranch

    And then

    git branch -m newbranch

  • If you want to, you can rename a branch when you’re working in another branch.

    git branch -m oldbranch newbranch

How to rename a remote branch in Git

If others use this branch and commit to it, you should pull it before renaming it locally. This ensures that your local repository is updated and that changes made by other users will not be lost.

  • First, we need to delete oldbranch from the remote repository, and push newbranch to the remote.

    git push origin --delete oldbranch

  • Now we’ll push the new one to the remote, by using -u (set upstream) option.

    git push origin -u newbranch

Upvotes: 30

loopassembly
loopassembly

Reputation: 3863

To rename your current branch to a new branch name:

git branch -m <new_name>

This will set the new name for the current branch you are working with.

To rename another branch:

git branch -m <old_name> <new_name>

Here you have to provide the old branch name and the new branch name.

Upvotes: 136

S. Hesam
S. Hesam

Reputation: 6603

Update 2024

Before we begin, make sure you’ve selected the branch you want to rename:

git checkout old-name

If you want to see all of your local branches, use the following command:

git branch --list

When you’re all clear, follow these steps:

  1. Using the Git rename branch command will require you to add an -m option to your command:

    git branch -m new-name
    
  2. You can also rename a local branch from another branch by using the following two commands:

    git checkout master
    
    git branch -m old-name new-name
    
  3. Lastly, this command will list all — both local and remote — branches to verify that it has been renamed:

    git branch -a
    

Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these two easy steps:

  1. To start, you need to rename a local branch by following the previous steps. 2.Then delete the old branch and push the new one. You can do this easily with the following command:

     git push origin :old-name new-name
    
  2. Reset the upstream branch for your new local branch, and you will be all set:

    git push origin -u new-name
    

In the end, as Nicolas Castro explained in the comments, to reset the upstream, run two commands respectively.

git branch --unset-upstream

git push --set-upstream origin new-name

Upvotes: 94

Vorac
Vorac

Reputation: 9084

Why does life need to be hard when it can be easy? Remembering -m is fine as long as you rename branches day and night. However my brain refuses to allocate space to remember that.

So I use
git branch new_name
git switch new_name # could have been git checkout -b
git branch -D old_name

Upvotes: 1

Madhan Ayyasamy
Madhan Ayyasamy

Reputation: 16538

You can rename a local Git branch using the following command:

git branch -m old_branch_name new_branch_name

Keep in mind that when you rename a branch, it still maintains its association with the old upstream branch if there was one.

To push changes to the master branch after renaming your local branch to new_branch_name, use the following command:

git push origin new_branch_name:master

With this command, your changes will be pushed to the master branch on the remote repository. However, your local branch will still be named new_branch_name.


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

Upvotes: 648

siride
siride

Reputation: 209435

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>

Upvotes: 19365

amit.flutter
amit.flutter

Reputation: 1111

Offical Command from Git Itself. Try this one. It works for me.

The default branch has been renamed! {oldBranchName} is now named {NewBranchName} If you have a local clone, you can update it by running the following commands.

git branch -m {oldBranchName} {NewBranchName}
git fetch origin
git branch -u origin/{NewBranchName} {NewBranchName}
git remote set-head origin -a

Upvotes: 3

Sambit Das
Sambit Das

Reputation: 151

  1. Rename a local branch:

    git branch -m <old_branch_name> <new_branch_name>

  2. Push the new branch:

    git push --set-upstream origin <new_branch_name>

Upvotes: 4

Tanah
Tanah

Reputation: 459

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.

Upvotes: 26

user13512643
user13512643

Reputation:

All you have to do are the three steps:

  1. Give the old branch under .git/refs/heads the new name
  2. Give the old branch under .git/logs/refs/heads the new name
  3. Update the .git/HEAD to lead to your new branch name

Upvotes: 1

Andrew
Andrew

Reputation: 1320

To rename a local branch on GitHub Desktop, click on the Current Branches tab (to the right of current repository), right click on the branch you want to rename and click on rename. You will then be prompted with a popup where you can rename.

enter image description here

Upvotes: 3

HoseiniPeyrov
HoseiniPeyrov

Reputation: 41

In Visual Studio:

Git → Manage Branches → Branches → your_repositoryyour_branch → Rename

Upvotes: -7

Pierre-Olivier Vares
Pierre-Olivier Vares

Reputation: 1759

Trying to answer specifically the question (at least the title).

You can also rename the local branch, but keep tracking the old name on the remote.

git branch -m old_branch new_branch
git push --set-upstream origin new_branch:old_branch

Now, when you run git push, the remote old_branch ref is updated with your local new_branch.

You have to know and remember this configuration. But it can be useful if you don't have the choice for the remote branch name, but you don't like it (oh, I mean, you've got a very good reason not to like it !) and prefer a clearer name for your local branch.

Playing with the fetch configuration, you can even rename the local remote-reference. i.e, having a refs/remote/origin/new_branch ref pointer to the branch, that is in fact the old_branch on origin. However, I highly discourage this, for the safety of your mind.

Upvotes: 71

AmerllicA
AmerllicA

Reputation: 32472

Actually you have three steps because the local branch has a duplicate on the server so we have one step for local on two steps on the server:

  1. Rename local: just use the following command to rename your current branch, even you checked it out:
    git branch -m <old-branch-name> <new-branch-name>
    
  2. Delete the server one: use the following command to delete the old name branch on the server:
    git push <remote-name[origin by default]> :<old-branch-name>
    
  3. Push the new one: now it's time to push the new branch named on the server:
    git push -u <new-branch-name>
    

Upvotes: 11

Hari_pb
Hari_pb

Reputation: 7386

Just three steps to replicate change in name on remote as well as on GitHub:

Step 1 git branch -m old_branchname new_branchname

Step 2 git push origin :old_branchname new_branchname

Step 3 git push --set-upstream origin new_branchname

Upvotes: 88

Vanchev
Vanchev

Reputation: 1584

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.

Upvotes: 142

Saad Bilal
Saad Bilal

Reputation: 1827

git branch -m [old-branch] [new-branch]

-m means move all from [old-branch] to [new-branch] and remember you can use -M for other file systems.

Upvotes: -4

Jethik
Jethik

Reputation: 1866

Advanced Git users can rename manually using:

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

Upvotes: 46

Dai Kaixian
Dai Kaixian

Reputation: 1065

All of the previous answers are talking about git branch -m. Of course, it's easy to operate, but for me, it may be a little hard to remember another Git command. So I tried to get the work done by the command I was familiar with. Yeah, you may guessed it.

I use git branch -b <new_branch_name>. And if you don't want to save the old branch now you can execute git branch -D <old_branch_name> to remove it.

I know it may be a little tedious, but it's easier to understand and remember. I hope it‘s helpful for you.

Upvotes: 9

Nomade
Nomade

Reputation: 1748

A simple way to do it:

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

For more, see this.

Upvotes: 21

AlexSi
AlexSi

Reputation: 168

In PhpStorm:

VCS → Git → Branches... → Local Branches → _your_branch_ → Rename

Upvotes: 0

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20137

Git branch rename can be done by using:

  1. git branch -m oldBranch newBranch

  2. git branch -M oldBranch ExistingBranch

The difference between -m and -M:

-m: if you're trying to rename your branch with an existing branch name using -m. It will raise an error saying that the branch already exists. You need to give unique name.

But,

-M: this will help you to force rename with a given name, even it is exists. So an existing branch will overwrite entirely with it...

Here is a Git terminal example,

mohideen@dev:~/project/myapp/sunithamakeup$ git branch
  master
  master0
  new_master
  test
* test1
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -m test1 test
fatal: A branch named 'test' already exists.
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -M test1 test
mohideen@dev:~/project/myapp/sunithamakeup$ git branch
  master
  master0
  new_master
* test
mohideen@dev:~/project/myapp/sunithamakeup$

Upvotes: 9

Vineet Jain
Vineet Jain

Reputation: 1575

If you want to:

  • Rename the Git repository, run: git branch -m <oldname> <newname>
  • Delete the 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 the status then use: git status
  • If you want to check out then use: git checkout

Upvotes: 4

Alireza
Alireza

Reputation: 104640

Changing the branch locally is quite easy...

If you are on the branch you want to change the name for, 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 on a command line. In this case, you are on master branch, for example:

Change branch name locally

Upvotes: 28

nikkypx
nikkypx

Reputation: 2005

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.

Upvotes: 17

Milind Anantwar
Milind Anantwar

Reputation: 82221

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

Upvotes: 404

dentuzhik
dentuzhik

Reputation: 752

To rename the 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'

Upvotes: 26

badarshahzad
badarshahzad

Reputation: 1297

  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

Upvotes: 43

Marcin Szymczak
Marcin Szymczak

Reputation: 11433

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

enter image description here

Upvotes: 23

Related Questions