447

If there is a repository that I only have git:// access to (and would usually just push+pull), is there a way to rename branches in that repository in the same way that I would do locally with git branch -m?

9
  • 52
    The linked "duplicate" question asks to rename a branch "both locally and remote". This question, however, only asks how to rename branches remotely, which allows for a simplification. This is what I do to rename a branch on the server without the need to checkout and / or create a local branch: git push origin origin/old_name:refs/heads/new_name && git push origin :old_name.
    – sschuberth
    Commented Nov 25, 2013 at 9:43
  • 1
    @sschuberth: you can give both commands in one go. And this really should be the answer to this question. Commented Dec 3, 2013 at 8:50
  • 2
    @JoachimBreitner You're right, I did that optimization already in this script of mine.
    – sschuberth
    Commented Dec 3, 2013 at 11:30
  • 1
    @sschuberth, you should post your comment as an answer, since I like it better than the others below.
    – phatmann
    Commented Jan 22, 2014 at 21:34
  • Done.
    – sschuberth
    Commented Jan 23, 2014 at 7:58

10 Answers 10

509

You just have to create a new local branch with the desired name, push it to your remote, and then delete the old remote branch:

$ git branch new-branch-name origin/old-branch-name
$ git push origin --set-upstream new-branch-name
$ git push origin :old-branch-name

Then, to see the old branch name, each client of the repository would have to do:

$ git fetch origin
$ git remote prune origin

NOTE: If your old branch is your main branch, you should change your main branch settings. Otherwise, when you run $ git push origin :old-branch-name, you'll get the error "deletion of the current branch prohibited".

11
  • 8
    Well, if the old and new names are the same, then it means you don't need to rename the branch, so there is no point running the command in the first place ;-) Commented Nov 8, 2014 at 20:25
  • 9
    Sure. I just mean that if you're calling this in an automated way (as a function part of some other script) you might as well not have it do the wrong thing if you can avoid it. Commented Nov 10, 2014 at 16:02
  • 9
    Dan's way: reorder the commands so they always just work. Earth Engine's way: always remember to check, or you lose data. I know which one I'd pick.
    – Doradus
    Commented Oct 27, 2015 at 15:01
  • 2
    The users can just run : git fetch origin --prune (to effectively fetch the new branches and also get rid of the references no longer on the remote). Commented Feb 19, 2016 at 14:49
  • 4
    Can use -d or --delete instead of : in newer versions of git.
    – Zitrax
    Commented Jun 2, 2016 at 9:38
324

If you really just want to rename branches remotely, without renaming any local branches at the same time, you can do this with a single command:

git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

I wrote this script (git-rename-remote-branch) which provides a handy shortcut to do the above easily.

As a bash function:

git-rename-remote-branch() {
  if [ $# -ne 3 ]; then
    echo "Rationale : Rename a branch on the server without checking it out."
    echo "Usage     : ${FUNCNAME[0]} <remote> <old name> <new name>"
    echo "Example   : ${FUNCNAME[0]} origin master release"
    return 1 
  fi

  git push $1 $1/$2\:refs/heads/$3 :$2
}

To integrate @ksrb's comment: What this basically does is two pushes in a single command, first git push <remote> <remote>/<old_name>:refs/heads/<new_name> to push a new remote branch based on the old remote tracking branch and then git push <remote> :<old_name> to delete the old remote branch.

16
  • 11
    For those who would like an alias of that command: rename = "!f() { git push origin origin/$1:refs/heads/$2 :$1; } ; f" this can be used as > git rename <old_name> <new_name> Commented Sep 3, 2014 at 12:08
  • 35
    For those curious about what this command actually means it's essentially 2 pushes git push <remote>/<old_name>:refs/heads/<new_name> means push a new remote that uses the old remote as a src then git push [space]:<old_name> means delete the old remote
    – ksrb
    Commented Sep 1, 2015 at 3:42
  • 3
    Why do you need to use refs/heads/name? Can't you just use name directly, making the first command git push <remote> <remote>/<old_name>:<new_name>? Commented Dec 20, 2016 at 9:20
  • 7
    No, because the remote branch <new_name> does not exist yet. If the branch does not exist, Git requires you to use the full name as otherwise <new_name> could also refer to a tag name.
    – sschuberth
    Commented Dec 20, 2016 at 9:24
  • 4
    We use this approach in our build system. The only caveat we encounter is if refs/heads/<new_name> already exists. The delete still succeeds, resulting in <remote>/<old_name> only being deleted. Some checking before hand can easily avoid this.
    – Apeiron
    Commented Apr 26, 2017 at 23:16
190

First checkout to the branch which you want to rename:

git branch -m old_branch new_branch
git push -u origin new_branch

To remove an old branch from remote:

git push origin :old_branch
2
  • 15
    When you push the renamed branch (new_branch) to remote (origin) you should also set its upstream to track the branch with the new name (e.g. git push -u origin new_branch) otherwise the renamed branch (new_branch) will continue to track the origin/old_branch. And once you delete the remote old_branch, the new_branch will still track the origin/old_branch, albeit now that branch is gone. Commented Feb 19, 2016 at 14:47
  • 1
    @DolphinDream I edited the answer to include your useful upstream change.
    – mVChr
    Commented Sep 15, 2016 at 18:51
10

Sure. Just rename the branch locally, push the new branch, and push a deletion of the old.

The only real issue is that other users of the repository won't have local tracking branches renamed.

1
  • 1
    so when trying to delete master, I've tried $ git clone ../src $ cd src $ git branch notmaster $ git checkout notmaster $ git branch -d master $ git push ../src :master But it complains: The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to '../alpha/' The remote really does have a branch called master
    – kdt
    Commented Jan 20, 2011 at 23:43
5

To change the branch name follow the below steps:

  1. git branch -m old_branchname new_branchname
  2. git push origin :old_branchname new_branchname
  3. git push --set-upstream origin new_branchname

After that, fetch the origin:

  1. git fetch origin
  2. git remote prune origin
1

I don't know if this is right or wrong, but I pushed the "old name" of the branch to the "new name" of the branch, then deleted the old branch entirely with the following two lines:

git push origin old_branch:new_branch
git push origin :old_branch
1
  • As far as I can tell, this is exactly what all the other answers does. Your answer is just more succinct.
    – Clearer
    Commented Jun 7, 2018 at 6:54
1

TL;DR

"Renaming" a remote branch is actually a 2 step process (not necessarily ordered):

  • deletion of the old remote branch (git push [space]:<old_name> as ksrb explained);
  • push into a new remote branch (difference between a couple of answers commands below).

Deleting

I use TortoiseGit and when I first tried to delete the branch through the command line, I got this:

$ git push origin :in
  • fatal: 'origin' does not appear to be a git repository

  • fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

This was likely due to pageant not having the private key loaded (which TortoiseGit loads automatically into pageant). Moreover, I noticed that TortoiseGit commands do not have the origin ref in them (e.g. git.exe push --progress "my_project" interesting_local:interesting).

I am also using Bitbucket and, as others web-based online git managers of the sort (GitHub, GitLab), I was able to delete the remote branch directly through their interface (branches page):

Delete branch Bitbucket

However, in TortoiseGit you may also delete remote branches through Browse References:

Browse References menu

By right-clicking on a remote branch (remotes list) the Delete remote branch option shows up:

TortoiseGit remote branch delete

Pushing

After deleting the old remote branch I pushed directly into a new remote branch through TortoiseGit just by typing the new name in the Remote: field of the Push window and this branch was automatically created and visible in Bitbucket.

However, if you still prefer to do it manually, a point that has not been mentioned yet in this thread is that -u = --set-upstream.

From git push docs, -u is just an alias of --set-upstream, so the commands in the answers of Sylvain (-set-upstream new-branch) and Shashank (-u origin new_branch) are equivalent, since the remote ref defaults to origin if no other ref was previously defined:

  • git push origin -u new_branch = git push -u new_branch from the docs description:

    If the configuration is missing, it defaults to origin.

In the end, I did not manually type in or used any of the commands suggested by the other answers in here, so perhaps this might be useful to others in a similar situation.

5
  • the problem is that your remote is not called origin. You have to name your remote as you get it from running the command git remote. Git works with ssh what implies that you're using public+private keys. I assume that the Autoload Putty keys of TortoiseGit is just autoloading the needed keys for you to do anything at all with your remote reference. Last thing is that git push -u is not an alias for pushing into a remote branch, it is an alias for pushing into a remote branch that was created locally and its remote reference has not yet this branch. Commented Mar 22, 2018 at 9:21
  • 1
    @juancab -u is an alias of --set-upstream and "if the configuration is missing, it defaults to origin". Sylvain and Shashank use this for pushing into a newly created remote branch. The key issue may have been due to pageant not having it loaded when I tried git push origin :in on the shell. So I don't understand your downvote, I just pointed out mine and non-addressed details in other answers, explained them and solved them.
    – CPHPython
    Commented Mar 22, 2018 at 15:24
  • You are stating wrong things and much of this answer is unrelated to the question itself. If you are pointing out what did work for you, I encourage you to limit the answer to what it did work and if you really want to give an explanation, please inform yourself better. Btw: -u is an alias for --set-upstream but that is not an alias for pushing into a remote branch as you said. To push into a remote branch you uniquely need git push <remote>, and if it's not in the remote yet, you add git push -u <remote>. Therefore, -u is used to create a reference of the branch in the remote. Commented Mar 22, 2018 at 17:44
  • 1
    @juancab perhaps what you deemed wrong was mostly the alias phrasing or choice of words. I restructured my answer and rephrased it in order to provide a full explanation of the solution(s) I found to rename a remote branch.
    – CPHPython
    Commented Mar 23, 2018 at 9:53
  • I would rephrase it further. It makes more sense now, but it is still too long. I would be more specific to the problem, i.e. state that for TortoiseGit users the proposed solutions won't work. You are telling a story and that's confusing and makes users avoid reading. I'll edit your answer with a proposition. Commented Mar 23, 2018 at 15:48
1

I don't know why but @Sylvain Defresne's answer does not work for me.

git branch new-branch-name origin/old-branch-name
git push origin --set-upstream new-branch-name
git push origin :old-branch-name

I have to unset the upstream and then I can set the stream again. The following is how I did it.

git checkout -b new-branch-name
git branch --unset-upstream
git push origin new-branch-name -u
git branch origin :old-branch-name
0

If you're using Github...

You can create a new branch based on old-name branch using the UI on github.com:

Branch Rename

1
  • 1
    That's GitHub, not Git. ;)
    – Bouncner
    Commented Jan 11, 2020 at 11:54
-4

Adding to the answers already given, here is a version that first checks whether the new branch already exists (so you can safely use it in a script)

if git ls-remote --heads "$remote" \
    | cut -f2 \
    | sed 's:refs/heads/::' \
    | grep -q ^"$newname"$; then
    echo "Error: $newname already exists"
    exit 1
fi
git push "$oldname" "$remote/$oldname:refs/heads/$newname" ":$oldname"

(the check is from this answer)

1
  • I would have used git show-ref --quiet --verify -- refs/heads/$new_name instead of ls-remote | cut | sed | grep.
    – Andy
    Commented Jun 29, 2017 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.