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!

Exactly the same issue here:

Exactly the same issue here: it should be
git push origin -u new_branch

Can you update it in the article?

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!

Except it doesn't remove the

Except it doesn't remove the branch under the old name so this is a copy not a rename.

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 would be cool to integrate

It would be cool to integrate that comment into the article itself then.

It was very helpful. Thank

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

You're a hero !!!!!!!

You're a hero !!!!!!!

Thanks

Really helpful and well-written post.

I didn't know you could delete remote branches using that shorthand approach. Much nicer than the way I've been doing it all these years.

Pull from renamed branch

Hi ,

This article was really helpful. I could able to rename the branch locally and remote.

But facing issue in doing pull request from newly renamed branch.

Many thanks for this!

Many thanks for this!

Thank you

Very helpful and easy to follow. Worked like a charm.

Still says "your branch is up to date with origin/OLDBRANCH"?

I thought I followed it correctly, but git status still says "Your branch is up-to-date with 'origin/OLDBRANCH'."
what did I do wrong?

% git branch -m OLDBRANCH NEWBRANCH

% git push origin :OLDBRANCH
To https://DELETED
- [deleted] OLDBRANCH
% git push origin NEWBRANCH
Total 0 (delta 0), reused 0 (delta 0)
To https://DELETED
* [new branch] NEWBRANCH -> OLDBRANCH
% git pull
Current branch NEWBRANCH is up to date.
% git status
On branch NEWBRANCH
Your branch is up-to-date with 'origin/OLDBRANCH'.
nothing to commit, working directory clean
% git push origin -u NEWBRANCH
Branch NEWBRANCH set up to track remote branch OLDBRANCH from origin by rebasing.
Everything up-to-date
%
% git status
On branch NEWBRANCH
Your branch is up-to-date with 'origin/OLDBRANCH'.
nothing to commit, working directory clean
% git pull
Current branch NEWBRANCH is up to date.
% git status
On branch NEWBRANCH
Your branch is up-to-date with 'origin/OLDBRANCH'.
nothing to commit, working directory clean
% git pull
Current branch NEWBRANCH is up to date.
%
% git status
On branch NEWBRANCH
Your branch is up-to-date with 'origin/OLDBRANCH'.
nothing to commit, working directory clean

missing part in last command

replace the last command, i.e
$ git push origin feature-15
with
$ git push --set-upstream origin feature-15

Thank you it was really

Thank you it was really helpful

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.