2

I want to rename branch from 'master' to 'introduction'. I found different links,but got me confused. Any help?

I tried this,but it didn't work.

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
Vampire
  • 35,631
  • 4
  • 76
  • 102
Theo
  • 361
  • 3
  • 5
  • 16
  • Possible duplicate of [Renaming a branch in github](http://stackoverflow.com/questions/9524933/renaming-a-branch-in-github) – Dario Dec 12 '16 at 15:29
  • 1
    @Dario how would that help? He did exactly what the accepted answer suggests but didn't have success. – Vampire Dec 12 '16 at 15:35
  • @Theo what does "didn't work" mean? Did you get some error message? – Vampire Dec 12 '16 at 15:35
  • @Vampire. I got to my github and I see the master branch. it is not renamed. – Theo Dec 12 '16 at 15:36
  • 1
    But did you get any errors when issuing those commands? Maybe you have to change the default branch on GitHub for this to work properly – Vampire Dec 12 '16 at 15:38
  • @Vampire. Can I change the branch name in github page? I didn't know that. – Theo Dec 12 '16 at 15:40
  • No, but you can change which branch is the default branch that is preselected when you go to the project. I think you cannot delete a branch in the remote as long as it is the default branch (like your local `HEAD`), I just thought you would get an error when trying to. – Vampire Dec 12 '16 at 15:41

2 Answers2

3

Go to https://github.com/<user>/<project>/settings/branches and change the default branch to being not the one you want to delete, then try again. You cannot delete the default branch in GitHub.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Cool. I can now see the introduction branch! Thanks for that. However,I believe that there should be a way to delete the master branch. I mean from the terminal(gitbash in my case). – Theo Dec 12 '16 at 15:46
  • 1
    Not as long as it is configured as the default branch in GitHub. GitHub does not allow to remove the default that is in use of course. ;-) – Vampire Dec 12 '16 at 15:47
0

1/ The correct sequence is:

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

# go to GitHub and select new_branch as your default one

git push origin :old_branch                 # Delete the old branch    

2/ Starting July 2020:

Links to deleted branches now redirect to the default branch

Previously, when branches were deleted on GitHub, any links that contained the old branch name broke across StackOverflow, email, Slack and other integrations.

Links to deleted branches now redirect to the default branch.

So, for example, the link https://github.com/dependabot/dependabot-core/blob/master/README.md will now redirect to an equivalent link on the default branch: https://github.com/dependabot/dependabot-core/blob/main/README.md.

This change only affects view links; other types of links (like edit links and blame links) don't redirect.

This change is the first of many changes GitHub is making to support projects and maintainers that want to rename their default branch.
To learn more about the changes we're making, see github/renaming.

3/ Starting Jan. 2021

This is directly supported.

See "Renaming a branch in GitHub"

rename branch dialog

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • GitLab, of course, is not far behind: https://about.gitlab.com/releases/2020/07/22/gitlab-13-2-released/#configurable-default-branch-name-for-new-repositories – VonC Jul 26 '20 at 00:09