Locally and Remotely Renaming a Branch in Git

Let's say you're busy working on your project, creating feature branches right and left to build out your super sweet app. Then your dev team decides to change their feature branch naming convention. How do you proceed without having an aneurysm? Can you easily accomodate your team's plan and not end up wanting to punch people in the face? Yep! As a matter of fact it's super simple to rename your branches. Here's how.

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

Then, you want to change the remote branch. This is a bit more complex, because you can't directly rename the remote branch. Instead you have to delete the remote branch with the old name, and recreate it with the new name. You need to start by getting the remote name:

git remote -v

This should give you something like this:

origin https://github.com/benjaminlhaas/Yii-with-Users-and-Rights (fetch)
origin https://github.com/benjaminlhaas/Yii-with-Users-and-Rights (push)

The remote name here is "origin." So now that you know this, you can delete and re-create the remote branches.

Delete the remote branch with the old name:
git push origin :my-hot-feature

Re-create the remote branch with the new name:
git push origin feature-15

(Note: this is only recommended if you don't have any other users working on this branch, or if you coordinate in advance with them. Check out this StackOverflow post for more info on the impact of renaming.)

That's all there is to it. Now enjoy your free time and go get an early lunch.

Category:
Tags:

Comments

Upstream / remote tracking missing?

Hi,

Thx for the post! Nice and clear reading - and it helped :)
One thing that might happen after that is if you try to push / pull changes to / from remote and you get:

Your configuration specifies to merge with the ref 'my-hot-feature'
from the remote, but no such ref was fetched.

that means you setup a remote tracking or upstream tracking before on the branch you just renamed. The tracking was not renamed...

git branch -u origin/feature-15

will fix that and point to the renamed remote branch.

best regards,
sploscho

Thanks

Awesome, thanks for the follow-up!

Thanks

Nice and easy to follow, thanks.

It was very helpful. Thank

It was very helpful. Thank you :-)

This post was extremely

This post was extremely helpful!!

Git can rename remote directly...

this is the first google result... so for those with google fu

"This is a bit more complex, because you can't directly rename the remote branch."

git checkout origin/current_remote_name -b current_remote_name
git push -u origin current_remote_name:new_remote_name

no need to delete the old branch!

Thanks

Thanks a lot. Come, let's eat lunch!

git pull warning

After doing these steps, I got the following message when I tried to do a git pull:

"Your configuration specifies to merge with the ref 'old_branch_name'
from the remote, but no such ref was fetched."

read above

Check out the first comment on this post.

It was very helpful. Thank

It was very helpful. Thank you :-)
http://www.hotellyonouest.com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.