Skip to content

Instantly share code, notes, and snippets.

@danieldogeanu
Last active February 2, 2026 05:53
Show Gist options
  • Select an option

  • Save danieldogeanu/739f88ea5312aaa23180e162e3ae89ab to your computer and use it in GitHub Desktop.

Select an option

Save danieldogeanu/739f88ea5312aaa23180e162e3ae89ab to your computer and use it in GitHub Desktop.
How to rename your Git master branch to main.

To rename your Git master branch to main, you must do the following steps:

  1. Navigate to your repository in the command line and issue the following commands: - git branch -m master main - git push -u origin main

  2. Change your default branch on GitHub by going to your GitHub repository in your browser, and navigate to Settings > Branches and click on the dropdown and switch from master to main and click Update (this will only show if you have two or more branches). The main branch is now your default branch.

  3. Update the tracking of the branch from your command line with the following command: - git branch -u origin/main main

  4. Delete the old master branch by going to your GitHub repository in your browser, navigate to your branches page, and click the Delete this branch button (the red trash bin icon). Your master branch is now gone.

If someone has a local clone of your repository, they can update their locals by following these steps:

  1. Got to the master branch: git checkout master
  2. Rename master to main locally: git branch -m master main
  3. Get the latest commits from the server: git fetch
  4. Remove the link to origin/master: git branch --unset-upstream
  5. Add a link to origin/main: git branch -u origin/main
  6. Update the default branch to be origin/main: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
  7. Delete origin/master completely: git branch --remotes -d origin/master

Thanks Scott Hanselman for the instructions, you can read the full article here: Easily rename your Git default branch from master to main

If this was useful, you can buy me a coffee here. Thank you!

@danieldogeanu
Copy link
Author

How do I change the name of default branch on a gist?

Honestly, I didn't even know you could do that. But I think you can manually rename the branch by cloning the gist to your local machine and pushing a new branch name:

  • Clone your Gist: git clone https://gist.github.com/<id>.git
  • Rename locally: git branch -m master main
  • Push the new branch: git push -u origin main
  • Delete the old one: git push origin --delete master

You can get the https://gist.github.com/<id>.git from the top-right part of the gist page, where it says Embed. You click on it and select Clone via SSH.

PS: These are AI instructions, I'm not sure that they work, I haven't tried them!

@ackvf
Copy link

ackvf commented Jan 30, 2026

I actually tried it before writing here - hence why I found this thread. If I delete master and push to main, everything disappears and the gist looks empty. Interestingly, when I pushed to a branch HEAD, it works. So I guess I can either use master or HEAD.

git push origin main:HEAD -u

This way I can use main locally and link it to the HEAD on the remote, without the need for master.

@danieldogeanu
Copy link
Author

I actually tried it before writing here - hence why I found this thread. If I delete master and push to main, everything disappears and the gist looks empty. Interestingly, when I pushed to a branch HEAD, it works. So I guess I can either use master or HEAD.

git push origin main:HEAD -u

This way I can use main locally and link it to the HEAD on the remote, without the need for master.

I think you are not following the order of operations, or maybe there are steps missing. You should create another branch with the name main, like when you create a normal feature branch (don't move the old master), and push that, set it as default, and then delete the old master. I can't try it myself right now, because I don't have the development environment set, I had to reinstall my OS recently.

@ackvf
Copy link

ackvf commented Feb 1, 2026

@danieldogeanu How do I set it as default? I know how to do it for a git repo, but how can I do it for a gist?

@danieldogeanu
Copy link
Author

@ackvf Perhaps this command will work? git remote set-head origin main

The workflow should be something like this:

  • Clone your Gist: git clone https://gist.github.com/<id>.git
  • Create new branch: git branch main
  • Push the new branch: git push -u origin main
  • Set new branch as default: git remote set-head origin main
  • Verify new branch: git branch -a (the default will be marked with origin/HEAD -> origin/main)
  • Delete the old one: git push origin --delete master

If the git remote set-head origin main doesn't work, you can try git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main instead, and see if it updates the server as expected.

I can't test it right now on my system, so hopefully it works. But why do you need such a thing anyway? I never thought that I should change the branch name for my Gists. Do you use some kind of app to read the gists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment