Skip to content

Branching & Merging

To work effectively as a team, engineers will leverage the power of branching and merging features in Git. The ease and effectiveness of branching, amplified by the pull request feature of Git servers, has popularized Git across the world of software development and has become the dominant version control system.

Branches

Branching allows engineers to work in isolation without disrupting the primary codebase. As multiple engineers work in parallel, each will make changes in their own branch independent of one another. Eventually they will want to integrate their code into the mainline. Branches should be short-lived and tied to work items.

Merging is the practice of reconciling changes between version controlled files from one branch to another branch. General merges can be performed effortlessly or automatically using a pull request. Merging should happen as frequently as possible to provide maximum flow, faster feedback, and minimize merge conflicts.

"Branching: a double-edged sword. If there was no branching, everybody would be editing the live code, half-baked changes would bork the system, people would be stepping all over each other. And so branches give individuals the illusion of frozen time, that they are the only ones changing the system and those changes can wait until they are fully baked before risking the system. But this is an illusion and eventually the price for it comes due. Who pays? When? How much? That's what these patterns are discussing: alternatives for paying the piper." - Kent Beck, from Martin Fowler's Patterns for Managing Source Code Branches

Branching Best Practices


Branch

When to create a branch?
A branch should be created only if required. This is most applicable when delivery teams have a history of creating long-lived release branches, whose changes often do not get merged into main.


Branch

When to merge changes?
Merging should happen as frequently as possible. As teams improve on creating smaller stories and engineers develop a clear way of working around pull requests, developers can merge code multiple times per day.


Branch

How to merge changes?
Changes should be merged to upstream branches through the remote's pull request functionality. This ensures quality of the application and shares knowledge of the application across the team.