Why does git push main work on GitHub when git push master does not? Also what is difference between Main branch and Master branch?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the early years of Git, the default branch name for a newly-created repository was `master`. However, in recent years, there has been a shift towards using `main` as the default branch name across many platforms, including GitHub. This change raises questions about why the command `git push main` works and `git push master` does not, along with what differences exist between the "Main branch" and the "Master branch."
Why Does "git push main" Work and Not "git push master" on GitHub?
Technical Explanation
The behavior of `git push main` working while `git push master` does not stems from the current default branch naming conventions and settings in a repository. When you create a new repository on GitHub, the default branch is now named `main`, not `master`. Therefore, if you attempt to push to `master` without first having created a branch named `master`, Git will not find the specified branch and the push will fail.
- Default Branch Name:
- main: Since October 2020, GitHub has adopted `main` as the default branch name for new repositories. This decision aligns with a broader movement towards more inclusive language in software development.
- master: This was the default branch name in the past but now needs to be explicitly set if desired.
- Branch Existence:
- Running `git push main` will work if:
- The repository was initialized with `main` as the default branch.
- A branch named `main` exists in the local clone.
- Running `git push master` will only work if:
- A branch named `master` has been created locally and in the remote.
- The repository settings allow for branches other than `main` as the default.
Example
Here's an example of a scenario where `git push main` works:
- Initialize a new repository on GitHub. It defaults to the `main` branch.
- Clone the repository:
- Nomenclature: Represents a modern, neutral, and inclusive naming convention.
- Default Behavior: Automatically set as default by platforms like GitHub for new repositories.
- Usage: Often represents the most stable and production-ready code in the repository.
- Nomenclature: More traditional; was the standard branch name since Git's inception in 2005 until the shift post-2020.
- Default Behavior: Requires manual setting to be the default branch in platforms like GitHub for new repositories.
- Usage: Like `main`, it can represent a stable production branch but is often seen now in legacy projects.

