Feature branch

A feature branch is a Git branch created from the main line for one feature or fix, where its commits collect until the change has been reviewed, tested and merged back. The feature branch workflow keeps unfinished work off the main branch, and the branch is deleted once it is merged. Most teams try to merge one within a few days.

The longer version

Atlassian's tutorial on the Feature Branch Workflow states the core idea: all feature development takes place in a dedicated branch instead of the main branch. The life of one is short and always the same. The developer updates main, creates the branch with git switch -c feature/coupon-field (or git checkout -b, which the tutorial uses), commits as often as the work needs, and pushes it with git push -u origin so that others can see it and the pipeline can build it. A pull request opens the review: colleagues comment, the author pushes fixes, the automated checks run against the branch, and once it is approved and free of conflicts it is merged and deleted. The tutorial recommends descriptive names such as animated-menu-items or issue-#1061, and many teams add a prefix such as feature/ or fix/ so that the branch list sorts itself. Where the branch starts depends on the model. In Gitflow a feature branch starts from develop and is merged back into develop; in GitHub flow and in trunk-based development it starts from main and returns to main.

How long the branch lives decides most of what goes wrong with it. Martin Fowler's "Patterns for Managing Source Code Branches" (28 May 2020) defines feature branching as putting all the work for a feature on its own branch and integrating it into the mainline when the feature is complete, and sets it against continuous integration, where developers integrate as soon as they have a healthy commit to share, usually less than a day's work. While a branch stays open, main keeps moving under it. Textual conflicts are the visible cost: Git stops and asks someone to choose between two edits of the same lines. Semantic conflicts are the hidden one. Fowler's example is one developer renaming a function while another adds a call to its old name: the text merges cleanly and the build still breaks. Paul Hammant's Trunk-Based Development site says a short-lived feature branch should only last a couple of days, should have one developer (two when pairing), and counts the wait for code review inside that time. Pulling main into the branch often, keeping each branch to one change and splitting a large feature into several merges keep the conflicts small. The merge itself can be a merge commit, which Gitflow makes with --no-ff so a feature's commits stay grouped, a squash into one commit, or a rebase.

Trunk-based development keeps the branch and shortens it. Developers merge to the trunk at least daily, and a feature that takes weeks reaches main in slices that users cannot see yet. Fowler names two ways to hide the unfinished slice: build the parts behind the scenes first and connect the user interface last, which he calls a keystone interface, or, where the partial feature cannot easily be hidden, use a feature flag. The flag moves the hiding from version control to runtime: the code is merged, tested and deployed, and stays switched off until someone turns it on. Long-lived feature branches and flags solve the same problem, and the flag avoids the large late merge at the cost of a second code path to test. A feature branch is also a different thing from a release branch. The feature branch adds behaviour and flows into the main line. A release branch is cut from the main line to stabilise one version and takes fixes only, and a hotfix branch starts from the code in production.

The branch is also the natural unit of review for people who do not read diffs. Many pipelines deploy every branch or pull request to a preview environment with its own address, rebuild it on each push and remove it at merge, so a designer, a product owner or a client can open the change in a browser before it touches the shared environments. That makes the preview the review point for how the feature looks and behaves, while staging still checks the merged release as a whole. Two habits keep that review honest. The reviewer checks that the preview was built from the latest commit, because a preview whose newest build failed keeps serving the previous one. And a bug found there is filed with the preview URL, the branch and the commit, because the address is gone once the branch merges and the same symptom may not exist on main.

Common questions

What is a feature branch in Git?

A feature branch is an ordinary Git branch used for one piece of work. Git treats it like any other branch; the convention is what makes it a feature branch. The branch is created from the main line, collects the commits for one feature or fix, is reviewed through a pull request, and is deleted after it is merged back.

What is the difference between a feature branch and a release branch?

A feature branch carries new work towards the main line and lives until that work is merged. A release branch is cut from the main line when a version is nearly ready, accepts only fixes and release preparation, and is the source of the build that ships. Features flow into the main line, and releases are stabilised off it.

How do you create a feature branch?

Update the main branch first with git switch main and git pull, then create and switch to the new branch with git switch -c feature/short-name, or git checkout -b feature/short-name on older habits. Commit on it, then push it with git push -u origin feature/short-name so the remote tracks it and a pull request can be opened.

How do I switch to a feature branch in Git?

Run git switch followed by the branch name, or git checkout with the name. For a branch that exists only on the remote, run git fetch first; git switch then creates a local branch that tracks the remote one of the same name. Commit or stash uncommitted changes before switching, or Git may refuse to move.

How long should a feature branch live?

As short a time as the work allows. The Trunk-Based Development site says a short-lived feature branch should only last a couple of days, counting the wait for review. A branch open for weeks drifts away from main, collects conflicts and merges as one large change that is hard to review and hard to revert.

What is the feature branch workflow?

The feature branch workflow is the routine of doing every change on its own branch: create it from main, commit, push, open a pull request, let the review and the automated checks run, merge, and delete the branch. Gitflow and GitHub flow both build on it, and differ in which branch the feature starts from.

Is a feature branch the opposite of trunk-based development?

Trunk-based development still uses feature branches, short ones. Developers merge to the trunk at least daily, and the branch exists for the code review and the build checks. Long-lived feature branches are what trunk-based development avoids, and unfinished work is hidden behind a feature flag on the trunk instead.

Should a feature branch be deleted after merging?

Deleting it is the normal end of its life, and most hosting platforms offer to do it when the pull request is merged. The commits stay in the history of main, so nothing is lost. Keeping old branches around only makes it harder to see which work is still open.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">