If you’re working on a project with GitHub, branching is an essential skill. Branches let you develop features, fix bugs, or experiment without affecting the main codebase. The master branch (or now often called main) is typically the stable version of your project. Creating a new branch from master is a common step before making changes.
In this guide, we’ll walk you through how to create a new branch from master using both the GitHub website and Git command line.
What Is a Branch?
A branch is a separate version of your codebase where you can make changes independently. This allows you to work on features or fixes safely without interrupting the main project.
Method 1: Creating a New Branch from Master on GitHub Website
- Go to your GitHub repository page.
Navigate to the repository where you want to create the branch. - Switch to the master branch (if needed).
Use the branch dropdown menu near the top-left, usually labeled with the current branch name, and select master. - Click the branch dropdown again.
At the top-left, click the branch selector dropdown. - Type the new branch name.
In the search box, type your desired new branch name (e.g.,feature/login-page
). - Create the branch.
A prompt will appear: “Create branch: your-branch-name from ‘master’.” Click this to create your branch. - Start working on your new branch!
Method 2: Creating a New Branch from Master Using Git Command Line
If you prefer working locally, here’s how to create and switch to a new branch based on master:
- Open your terminal or command prompt.
- Navigate to your project directory.
cd path/to/your/repository
- Fetch the latest changes and switch to master.
git fetch origin git checkout master git pull origin master
- Create and switch to the new branch.
git checkout -b your-new-branch-name
Replaceyour-new-branch-name
with your preferred branch name. - Push the new branch to GitHub.
git push -u origin your-new-branch-name
Tips for Branch Naming
- Use descriptive names like
feature/login
,bugfix/header-error
, orhotfix/security-patch
. - Use hyphens or slashes for readability.
- Keep it consistent with your team’s naming conventions.
Why Create Branches from Master?
The master branch is often the most stable and updated version of your project. Starting a new branch from master ensures your new work is based on the latest, stable code, reducing conflicts and bugs.
Summary
Creating a new branch from master is straightforward and crucial for effective collaborative development. Whether using the GitHub web interface or Git command line, it allows you to safely develop new features and fixes.