56

I don't know how to run command line stuff. I just don’t have the environment.

So I'm trying to rename a branch on the GitHub website. It was, by default, named patch-1.

Is it possible to rename it on the site?

Lucas
  • 523
  • 2
  • 10
  • 20
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    No, not really. And you'll be limited on Github with other features such as merging conflicts without a command-line or graphical VCS environment. – nanofarad May 24 '14 at 23:29
  • Just push it back up with a different name, and delete the original ? – Tony Hopkinson May 24 '14 at 23:33
  • I was thinking of doing that but i asked here and it seems we cannot truly delete a branch: http://stackoverflow.com/questions/23850490/github-delete-branch-even-after-made-commits-to-it/23850519?noredirect=1#23850519 – Noitidart May 24 '14 at 23:34
  • 1
    Since Jan. 2021, this is officially supported. See [my answer below](https://stackoverflow.com/a/65799365/6309) – VonC Jan 19 '21 at 20:53

7 Answers7

30

I just did it without downloading any code to my laptop and using only the GitHub site. The solution looks the same as @swcool’s, but I want to add about the default branch.
In my case, the name of the renaming branch did not exist.

  1. Change the default branch (to the old branch you want to rename)

  2. Create a new branch (with a new name)

    This action will copy all the contents of the default branch (the branch with the old name) to the new branch (with a new name). At this time, you have two branches with the same code.

  3. Change the default branch (to the new one with a new name)

  4. Delete the old branch

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
allenhwkim
  • 27,270
  • 18
  • 89
  • 122
  • 1
    In step 3, isn't it a better idea to just change the default branch back to `master`? Or am I missing something? – dev Apr 24 '20 at 21:44
  • @edjroot - it depends on which branch you're renaming. If, for example, you want to rename the `master` branch to `main`, then you'll need to change the default branch to `main`. Admittedly, that wasn't asked in the question, but it's a useful detail to note for anyone reading about renaming and changing branches. – Myles Aug 30 '20 at 13:25
26

I think you can, just create a new branch with the new name, and delete the old one on github.

More detail you can see here.

swcool
  • 1,003
  • 9
  • 10
  • 34
    That wasn't really the question, was it? – Kutzi Mar 22 '16 at 07:53
  • 3
    @Kutzi It kind of is tho. You create a new branch from the one you'd like to change. – shangxiao Jun 09 '17 at 09:16
  • 3
    It's not a useful answer if, for instance, the branch has an open PR and you'd like to preserve the discussion left on that PR. – Eric Fulmer Aug 01 '18 at 14:12
  • 3
    You can now change the target branch in an open PR. So I'd say this is an option anyway. – mrpasqal Aug 16 '18 at 11:23
  • 2
    And will still lead to every link to the branch code in any github issues/comments etc to be broken, so no, this is not the answer. It's a workaround, but the real answer is "you can't, github does not support _renaming_ branches, but you can [and then this answer] instead". – Mike 'Pomax' Kamermans Aug 16 '20 at 15:57
16

It is not possible to rename a branch from the Github website. You will need to do the following -

Setup your Git Environment

Follow this - https://help.github.com/articles/set-up-git

Rename branch locally & on Github

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

Nitish Dhar
  • 2,282
  • 15
  • 18
8

If you don't want to install Git, clone the repo, rename the branch locally and push it back to GitHub, you can use the GitHub API for references:

  • create a new branch where the old one is:

    POST /repos/:owner/:repo/git/refs
    
    {
      "ref": "refs/heads/newBranchName",
      "sha": "<SHA1 of old branch>"
    }
    
  • delete the old branch:

    DELETE /repos/:owner/:repo/git/refs/heads/oldBranchName
    

That way, you will have "renamed" (create+delete) the branch without having git locally.

And, as commented by user3533716 below, use the GitHub API for listing branches to get those branch SHA1:

GET /repos/:owner/:repo/branches
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • SHA1 of what? old branch name? – Nakilon Sep 23 '14 at 23:15
  • Can't copy master, says `404 Not Found`: https://lh5.googleusercontent.com/-9WxBOLc--yk/VCIBf46NGhI/AAAAAAAATTI/rQArVKCnjmo/s0/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%2B%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%2B2014-09-24%2B%D0%B2%2B3.25.27.png – Nakilon Sep 23 '14 at 23:27
  • @Nakilon yes, SHA1 of the old branch: `git rev-parse ` – VonC Sep 24 '14 at 05:22
  • @Nakilon you would need to be authenticated for this POST to succeed. No authentication means 404 ("not found"). See https://gist.github.com/caspyin/2288960 – VonC Sep 24 '14 at 05:25
  • OP wants to rename the branch "from the web site". API usage is far beyond that requirement. – Dan Dascalescu Jul 01 '16 at 05:38
  • To list branches, and get the SHA1, use:`GET /repos/:owner/:repo/branches` – user3533716 Mar 18 '17 at 20:17
  • @user3533716 Thank you. I have included your comment in the answer for more visibility. – VonC Mar 18 '17 at 20:22
7

Since Jan., 19th 2021, you now can rename a branch directly on github.com:

Support for renaming an existing branch:

You can now rename any branch, including the default branch, from the web.

Branch rename dialog -- https://i2.wp.com/user-images.githubusercontent.com/2503052/105069955-a231fa80-5a50-11eb-982c-a114c9c44c57.png?ssl=1

If you've been waiting to rename your default branch from master to main, we now recommend doing so using this feature.

When a branch is renamed:

  • Open pull requests and draft releases targeting the renamed branch will be retargeted automatically
  • Branch protection rules that explicitly reference the renamed branch will be updated

Note: admin permissions are required to rename the default branch, but write permissions are sufficient to rename other branches.

To help make the change as seamless as possible for users:

  • We'll show a notice to contributors, maintainers, and admins on the repository homepage with instructions for updating their local repository
  • Web requests to the old branch will be redirected
  • A "moved permanently" HTTP response will be returned to REST API calls
  • An informational message will be displayed to Git command line users that push to the old branch

This change is one of many changes GitHub is making to support projects and maintainers that want to rename their default branch.

Branch names will not change unless the maintainer explicitly makes the change, however this new rename functionality should dramatically reduce the disruption to projects who do want to change branch names.

To learn more about the change we've made, see github/renaming.

To learn more, see Renaming a branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

To rename a branch on the Github website, just go to your repo's home page, click on where it says "branches"

enter image description here

Then, find the branch you're interested in, click the pencil button

enter image description here

and from there, you can rename your branch. enter image description here

Chris Gong
  • 8,031
  • 4
  • 30
  • 51
-2

If you want a GUI based solution - download the Git Client "GitKraken". It supports doing this from UI by right-clicking on the branch name and choosing "rename [branch name]".

Adam Diament
  • 4,290
  • 3
  • 34
  • 55