Git Alias to Create Feature Branch from Main
Create a git alias that fetches the latest main branch and creates a new feature branch from it in a single command for consistent branching.
Detailed Explanation
Feature Branch from Fresh Main
One of the most common workflow patterns is creating a new feature branch based on the latest main branch. This shell alias automates the multi-step process:
[alias]
feature = !git fetch origin main:main && git switch -c
Usage
git feature my-new-feature
This single command:
- Fetches the latest
mainfrom origin and updates the localmainbranch - Creates a new branch called
my-new-featurebased on the current HEAD - Switches to the new branch
Why Use a Shell Alias?
The ! prefix tells git to run this as a shell command rather than a git subcommand. This enables chaining multiple git commands with &&, using pipes, and passing arguments.
Alternative: Always Branch from Main
If you want to always branch from the updated main, regardless of your current branch:
[alias]
feature = !git fetch origin && git switch -c $1 origin/main
This variant fetches all remotes and then creates the new branch directly from origin/main, ensuring your feature branch always starts from the latest remote main.
Why This Pattern Matters
Starting feature branches from an outdated main is a common source of merge conflicts. By automating the fetch-and-branch pattern, you ensure every new feature branch begins from the latest codebase. This reduces merge conflicts and keeps your branch topology clean.
Team Convention
When your entire team uses this alias, branch creation becomes consistent. Everyone starts from a fresh main, which means fewer surprises during code review and merge.
Use Case
Use this alias at the start of every new task or user story in a feature-branch workflow. It is especially valuable in fast-moving teams where main receives multiple merges per day and starting from a stale base would cause immediate conflicts.
Try It — Git Alias Builder
Related Topics
Git Alias for Checkout and Branch Shortcuts
Branch Aliases
Git Aliases for Branch Listing and Management
Branch Aliases
Git Aliases for WIP (Work in Progress) Workflow
Workflow Aliases
Git Alias to Prune Stale Remote Tracking Branches
Cleanup Aliases
Git Alias Best Practices and Organization Tips
Best Practices