Sub topic 7
Matching the default branch name
This chapter assumes you have Git installed with user.name and user.email set, you can make commits with git add and git commit, and you have a local repository with at least one commit. It also assumes you have an empty GitHub repository and have added it as origin with git remote add. The chapter covers why branch names matter before your first push and how to make them match.
Why the branch name matters
A branch is a movable pointer to a commit. Your local repository has a current branch, and GitHub has a default branch. The default branch is the one GitHub shows first, and it is the one new clones check out.
Git does not require the two names to match. If you push a local branch called master to GitHub, GitHub creates a branch called master. If its default setting is main, you end up with a main that doesn't exist yet and a master that people don't expect to see. Matching the names before the first push avoids this.
The default has changed over time. Older Git versions create master on git init. Newer versions use master unless you configure otherwise. GitHub has used main for new repositories since October 2020. So a repository created locally and a repository created on GitHub often disagree.
Check your current branch
Run this in your local repository:
git branch --show-currentThe command prints the name of the checked-out branch, such as master or main. If the output is empty, you are in a detached HEAD state, which means HEAD points at a commit instead of a branch. Switch to a branch before you continue.
Rename the local branch
To rename the current branch to main, use:
git branch -M mainThe -M option is a forced rename. It moves the branch and overwrites an existing branch named main if one is there. The commits stay the same. Only the label changes.
If you prefer a lowercase -m, Git refuses to rename when the target name already exists. That is safer when you're unsure whether another main is present.
Set the default for future repositories
You can make git init use main from the start. This setting works from Git 2.28 onward:
git config --global init.defaultBranch mainIt only affects repositories you create after setting it. Existing repositories keep their current branch name, so you still run git branch -M main on those.
Use a different name on purpose
Some teams use trunk or develop. In that case, use the same name in two places: your local branch and the GitHub default branch. On GitHub, open the repository, choose Settings, then Branches, and change the default branch there. An empty repository has no branches yet, so GitHub only fixes the name after your first push creates one.
If the names cannot match, you can push under a different remote name:
git push -u origin master:mainThe part after the colon is the remote branch name. This sends local master to remote main. It works, but the local and remote names then differ, which confuses people. Renaming is usually clearer.
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| `error: src refspec main does not match any` | No local branch called `main` exists, or there are no commits | Run `git branch -M main`, and make sure you committed |
| Two branches appear on GitHub | You pushed `master` to a repository that expects `main` | Rename locally, push `main`, then delete `master` on GitHub |
| Empty output from `git branch --show-current` | Detached HEAD | Run `git switch main` or create a branch |
Order of operations
- Make at least one commit.
- Check the name with
git branch --show-current. - Rename with
git branch -M mainif needed. - Push with
git push -u origin main, which the next chapter explains.
Exercise
Your local repository's branch is master, and your empty GitHub repository expects main. Which command makes the names match without changing any commits, and why is it better to run it before the first push?
Show answer
Run git branch -M main. It only renames the branch pointer, so all commits stay unchanged. Running it before the first push means GitHub receives a branch named main immediately, so you avoid creating a second branch called master that you would have to delete later.