DEV Community

Aman Mittal
Aman Mittal

Posted on • Originally published at amanhimself.dev

5 2

How to rename default branch in Git and GitHub

I'm currently maintaining a GitHub repo that is grown over the last year. It primarily contains all the example code that I create when working on React Native and Expo articles and tutorials.

Recently, I switched the default branch to main. Here are the steps I took. All of these commands execute inside a terminal window.

Start by moving the default branch locally using the -m flag.

git branch -m master main
Enter fullscreen mode Exit fullscreen mode

Set the new branch as the local default on the remote. By default, Git doesn't allow renaming a remote branch.

git push -u origin main

# output
* [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Enter fullscreen mode Exit fullscreen mode

Set the current local HEAD branch to point to the new branch on GitHub.

git remote set-head origin main
Enter fullscreen mode Exit fullscreen mode

Now, go to the GitHub repo. Open Settings > Branches. Under Default Branch, click the edit button to switch to the main branch.

ss1

Now, go back to the terminal window and run the following command to delete the previous old default branch.

git push origin --delete master

# output
To https://github.com/account/repo.git
- [deleted]         master
Enter fullscreen mode Exit fullscreen mode

That's it!

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay