Renaming a branch in GitHub
Asked Answered
G

18

418

I just renamed my local branch using

git branch -m oldname newname

but this only renames the local version of the branch. How can I rename the one on GitHub?

Giantism answered 1/3, 2012 at 22:7 Comment(6)
possible duplicate of git: rename remote branchGosser
Have you checked in the admin settings? My current project on github doesn't have any branches but you can rename the repo there so I would think you could rename branches as well.Borneol
@Borneol Nope, you cannot rename a Git branch from the GitHub site.Gosser
The answers for this question are nearly universally wrong. The question is not how to rename a git branch, it's how to rename a github branch. Many things on github are tied to a branch which means any answer needs to cover those things. For example renaming master to mainCircum
@Circum For what it's worth, it seems these answers answered the spirit of the OP's question. The OP's question may not have been perfectly phrased, from a technically exacting standpoint, or the relevant issue you are currently looking into. But then, this question isn't coming from a deep technical background of either git or GitHub. From this perspective, one can consider this a "beginner" or elementary question. As such, it is phrased, & answered appropriately for this audience. Frustrating, sure, but I think this Q, these Answers are appropriate/relevant, esp for pre BLM git/Hub usage Q's.Lianeliang
If the answers are only git then this entire question should be close as a duplicate of this one. The only thing that makes it not a duplicate is it's about github not git and therefore the answers need to cover the differences between git and github.Circum
D
392

As mentioned, delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:

git push origin :name_of_the_old_branch_on_github
git push origin new_name_of_the_branch_that_is_local

Dissecting the commands a bit, the git push command is essentially:

git push <remote> <local_branch>:<remote_branch>

So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.

As of Git 1.7 there is an alternate syntax for deleting a remote branch:

git push origin --delete name_of_the_remote_branch

As mentioned by @void.pointer in the comments

Note that you can combine the 2 push operations:

git push origin :old_branch new_branch

This will both delete the old branch and push the new one.

This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig:

[alias]
    branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #"

Usage:

git branchm origin old_branch new_branch

Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version. See this discussion for details.

Digestant answered 1/3, 2012 at 23:21 Comment(12)
Note that you can combine the 2 push operations: git push origin :old_branch new_branch. This will both delete the old branch and push the new one.Salinasalinas
Is there any reason why the new branch can't be pushed before deleting the old? I personally prefer processes where the delete operation happens after the create operation is successful, just in case something goes wrong.Sprite
Really good question, and yeah, you do have to delete the old branch first (otherwise it'll fail as it's not a FF update). If you're concerned though you can just create a copy of the old branch before starting.Digestant
Does not work. It will simply put the branch back on Github with its old name.Inveteracy
@geoff: seems to work for me, could you be more specific?Digestant
@AdamParkin 1. git branch -m new_branch (rename old_branch to new_branch) 2. git commit -m 'msg', 3. git push 4. Mhmm, old_branch shows up in Github, Google question and I am led to your answer 5. git push origin :old_branch (says it deleted) 6. git push origin new_branch... completes then says * [new branch] new_branch -> old_branch. Go back to Github and old_branch shows up again. If I delete in Github web UI, I have the option to "Restore," so it seems like pushing the new_branch is just restoring.Rabi
If I would be removing the old branch and making a new branch, wouldn't that also be making multiple commits on the remote?Scholastic
No, this isn't Mercurial, making a branch does not introduce a new commit.Digestant
Note that this is essentially creating a new branch with the same commits. For example, if you have an open pull request with the old branch, you will have to make another one from the new branch.Chrysanthemum
One thing to be aware of is that when you rename the branch, any pull requests you have open against that pull request will be automatically closed. We had to rename the branch back and then reopen manually all of the pull requests....Sprite
@Inveteracy this worked on github: git branch -m oldbranch newbranch; git push origin --delete oldbranch; git push origin newbranch:newbranchLengthen
To github.com:user/user.github.io.git * [new branch] main -> main ! [remote rejected] master (refusing to delete the current branch: refs/heads/master) error: failed to push some refs to 'github.com:username/username.github.io.git' it doesn't seem to work if you only have 1 branch - master in my case.Picturize
B
60

The following commands worked for me:

git push origin :old-name-of-branch-on-github
git branch -m old-name-of-branch-on-github new-name-for-branch-you-want
git push origin new-name-for-branch-you-want
Berty answered 4/5, 2015 at 11:46 Comment(7)
This is the brief answer that works. BTW, the first command could also be put as last one in the answer.Thurible
Actually, I just did this twice and it worked on github.Darlington
Should be the accepted answer - nice and simple. Thanks!Buddie
Important to mention that you need to have the branch checked out, or else the first command just deletes the remote branch.Vociferant
how does this impact child branches of old-name-of-branch-on-github ? Will they become children of new-name-for-branch-you-want ?Overbold
can i delete the old branch after following these steps?Jeannettejeannie
yes, you can delete that branchBerty
R
42

I've found three commands on how you can change your Git branch name, and these commands are a faster way to do that:

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 step-by-step you can read this great article:

How to Rename Git Local and Remote Branches

Rivy answered 10/5, 2016 at 18:7 Comment(1)
git push --set-upstream is the most important part if you happen to change the branch name locally using github app before deleting remote branch.Library
B
26

Rename branches in Git local and remote

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.

Switch to the branch and then:

git push origin -u new-name

So the conclusion is:

git branch -m new-name
git push origin :old-name new-name
git push origin -u new-name
Bock answered 11/9, 2019 at 13:29 Comment(3)
This is for git and GitHub is under git so it will work and it works with me and with othersBock
It doesn't work. Try first and second steps git branch -m new-name && git push origin :master new-name. It will fail. If this answer is only about git and not github then this question is a duplicate of several other questions and should be closed. If it really is about github then the answer has to cover github. This answer does not.Circum
For me I only needed to do steps 1 and 2, and this process workedAldana
B
17

You can do that without the terminal. You just need to create a branch with the new name, and remove the old after.

Create a branch

In your repository’s branch selector, just start typing a new branch name. It’ll give you the option to create a new branch:

Create a branch

It’ll branch off of your current context. For example, if you’re on the bugfix branch, it’ll create a new branch from bugfix instead of master. Looking at a commit or a tag instead? It’ll branch your code from that specific revision.

Delete a branch

You’ll also see a delete button in your repository’s Branches page:

Delete a branch

As an added bonus, it’ll also give you a link to the branch’s Pull Request, if it has one.

I just copy and paste this content from: Create and delete branches

Bihar answered 19/8, 2015 at 14:41 Comment(0)
A
14

Just remove the old branch and create new one.

Example (solely renaming the remote branch):

git push origin :refs/heads/oldname
git push origin newname:refs/heads/newname

You also probably should rename local branch and change settings for where to push/pull.

Alkoran answered 1/3, 2012 at 22:46 Comment(6)
Shouldn't the 2nd command be git push origin newname:refs/heads/newname ? master and newname may not point to the same commit.Kedge
"master" is just as example, will change now.Alkoran
Maybe the order of two commands should be reversed (i.e. upload new name first, remove old after that)Alkoran
@gman, Doesn't renaming it Git-wise also rename it Github-wise?Alkoran
Try it. git push origin :refs/heads/masterCircum
@gman, Should remove master branch, unless it is marked as protected.Alkoran
M
14

On GitHub side, you can use the new (Jan. 2021) "Support for renaming an existing branch" (protected branches can only be renamed by admins, see the end)

Follow this tutorial: https://docs.github.com/en/github/administering-a-repository/renaming-a-branch

rename branch dialog -- https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpLi21bC2jaRA/user-images.githubusercontent.com/2503052/105069955-a231fa80-5a50-11eb-982c-a114c9c44c57.png?ssl=1

See "How do I rename branch on the GitHub website?".

This is a better approach, because renaming a branch that way (on github.com) will (source):

  • Re-target any open pull requests
  • Update any draft releases based on the branch
  • Move any branch protection rules that explicitly reference the old name
  • Update the branch used to build GitHub Pages, if applicable
  • Show a notice to repository contributors, maintainers, and admins on the repository homepage with instructions to update local copies of the repository
  • Show a notice to contributors who git push to the old branch
  • Redirect web requests for the old branch name to the new branch name
  • Return a "Moved Permanently" response in API requests for the old branch name

Update Dec. 2021:

Restrict renaming protected branches to admins

Now, only admins can rename branches that are protected by branch protection rules.

GitHub allows repository collaborators to rename every branch in a repository, with the exception of the default branch.

When a collaborator renames a branch, any non-wildcard branch protection rules that apply to that branch are also changed to match the branch's new name.

Because only admins can modify branch protection rules, renaming of a protected branch is now limited to admin users.

For more information, visit Renaming a branch and Managing a branch protection rule.

Maxwell answered 19/1, 2021 at 20:56 Comment(1)
Unfortunately as of writing, the "re-target any open pull requests" part is only true for "base" branches, and "regular" branches associated to a pull-request will still close the PR in question.Malcah
G
11

Simple as that. In order to rename a Git branch locally and remotely use this snippet (tested and works like a charm):

git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>

Explanation:

  1. Rename step:

Git reference: 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.

  1. Delete step:

Git reference: git push origin :experimental Find a ref that matches experimental in the origin repository (e.g. refs/heads/experimental), and delete it.

  1. Update on remote repository step (upstream reference for tracking):

Git reference: --set-upstream For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull[1] and other commands. For more information, see branch.<name>.merge in git-config[1].

Gilus answered 31/10, 2019 at 15:34 Comment(0)
P
3

Here is what worked for me:

  1. Create the new branch first:

    git push github newname :refs/heads/newname
    
  2. On the GitHub site, go to settings and change the Default branch to newname

  3. Delete the oldname

    git push github --delete oldname
    
Pairs answered 4/6, 2015 at 7:10 Comment(0)
C
3

This is an added condition in Hazarapet Tunanyan's answer.

git branch -m old_branch new_branch         # Rename branch locally


git push origin :old_branch                 # Delete the old branch
# You might be getting an error doing the above step, skip to the next step

git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

You get an error doing git push origin :old_branch because old_branch you are trying to delete might be the default branch.

Just do the other 2 steps and then goto github and change the default branch from the settings, then you will be able to do git push origin :old_branch.

Coralloid answered 14/5, 2021 at 14:26 Comment(0)
I
2

Another way is to rename the following files:

  1. Navigate your project directory.
  2. Rename .git/refs/head/[branch-name] to .git/refs/head/new-branch-name.
  3. Rename .git/refs/remotes/[all-remote-names]/[branch-name] to .git/refs/remotes/[all-remote-names]/new-branch-name.

Rename head and remotes both on your local PC and on origins(s)/remote server(s).

Branch is now renamed (local and remote!)


Attention

If your current branch-name contains slashes (/) Git will create the directories like so:

current branch-name: "awe/some/branch"

  • .git/refs/head/awe/some/branch
  • .git/refs/remotes/[all-remote-names]/awe/some/branch

wish branch-name: "new-branch-name"

  1. Navigate your project directory.
  2. Copy the branch file from .git/refs/*/awe/some/.
  3. Put it in .git/refs/head/.
  4. Copy the branch file from all of .git/refs/remotes/*/awe/some/.
  5. Put them in .git/refs/remotes/*/.
  6. Rename all copied branch files to new-branch-name.
  7. Check if the directory and file structure now looks like this:
  • .git/refs/head/new-branch-name
  • .git/refs/remotes/[all-remote-names]/new-branch-name
  1. Do the same on all your remote origins/servers (if they exist)
  • Info: on remote-servers there are usually no refs/remotes/* directories because you're already on remote-server ;) (Well, maybe in advanced Git configurations it might be possible, but I have never seen that)

Branch is now renamed from awe/some/branch to new-branch-name (local and remote!)

Directories in branch-name got removed.


Information: This way might not be the best, but it still works for people who might have problems with the other ways

Ical answered 10/8, 2016 at 13:57 Comment(0)
J
1

This article shows how to do it real easy.

  1. To rename a local Git branch, we can use the Git branch -m command to modify the name:

     git branch -m feature1 feature2
    
  2. If you’re just looking for the command to rename a remote Git branch, this is it:

     git push -u origin feature2:feature3
    

    Check that you have no tags on the branch before you do this. You can do that with git tag.

Jehu answered 15/11, 2015 at 3:11 Comment(1)
What does "This article" refer to? Please respond by editing your answer, not here in comments.Pretzel
L
1

In my case, I needed an additional command,

git branch --unset-upstream

to get my renamed branch to push up to origin newname.

(For ease of typing), I first git checkout oldname. Then run the following:

git branch -m newname <br/> git push origin :oldname*or*git push origin --delete oldname
git branch --unset-upstream
git push -u origin newname or git push origin newname

This extra step may only be necessary because I (tend to) set up remote tracking on my branches via git push -u origin oldname. This way, when I have oldname checked out, I subsequently only need to type git push rather than git push origin oldname.

If I do not use the command git branch --unset-upstream before git push origin newbranch, git re-creates oldbranch and pushes newbranch to origin oldbranch -- defeating my intent.

Lianeliang answered 3/6, 2017 at 5:50 Comment(2)
@Circum Point taken. My solution uses git locally to rename a branch on GitHub, remotely, after renaming it on the local git copy of the GitHub repository. So perhaps it could be considered a specialized solution, for when you want to use the command line, rather than the GitHub website interface, and you have a local clone that you also would like updated. Thanks for highlighting this distinction, and clarifying that this answer is for a special case, rather than a general solution.Lianeliang
The problem is github ties features to branches. Try renaming master to main and your instructions will fail.Circum
P
1

Branch Rename is now available through GitHub API

You can rename a branch with the GitHub REST API.

And you can easily run API commands via the gh CLI all like this:

gh api "repos/{owner}/{repo}/branches/{branch}/rename" -f new_name={newBranch}
Pym answered 18/2, 2022 at 21:39 Comment(0)
R
-1
  1. Download Atlassian Sourcetree (free).
  2. Import your local clone of the repository.
  3. Right click your branch to rename, in the sidebar. Select "Rename branch..." from context menu, and rename it.
  4. Push to origin.
Reducer answered 25/8, 2015 at 20:45 Comment(2)
Thanks, but I had to switch to the branch as well as importing it.Uitlander
The link is broken (404).Pretzel
N
-1

The following commands rename the branch locally, delete the old branch on the remote location and push the new branch, setting the local branch to track the new remote:

git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch
Novelize answered 15/3, 2018 at 10:47 Comment(1)
Although your code snippet might solve the issue, you should describe what’s the purpose of your code (how it solves the problem). Furthermore, you might want to check stackoverflow.com/help/how-to-answerHydride
I
-1

On the Git branch, run:

git branch -m old_name  new_name

This will modify the branch name on your local repository:

git push origin :old_name new_name

This will push the modified name to the remote and delete the old branch:

git push origin -u new_name

It sets the local branch to track the remote branch.

This solves the issue.

Inanition answered 17/5, 2020 at 1:4 Comment(0)
P
-2

Three simple steps

  • git push origin head

  • git branch -m old-branch-name new-branch-name

  • git push origin head

Pronty answered 9/4, 2020 at 11:46 Comment(1)
Doesn't work. All it did is create a new branch on github. It didn't rename the branch. pastebin.com/dDh06HEbCircum

© 2022 - 2024 — McMap. All rights reserved.