Adopt a Git branching strategy

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2013

Distributed version control systems like Git give you flexibility in how you lot use version control to share and manage code. Your team should find a residue between this flexibility and the need to collaborate and share code in a consistent manner.

Squad members publish, share, review, and iterate on code changes through Git branches shared with others. Prefer a branching strategy for your team. You can collaborate ameliorate and spend less time managing version control and more fourth dimension developing code.

The following branching strategies are based on the way we use Git here at Microsoft. For more information, see How nosotros use Git at Microsoft.

Keep your branch strategy simple

Keep your branch strategy uncomplicated. Build your strategy from these three concepts:

  • Use feature branches for all new features and bug fixes.
  • Merge characteristic branches into the main co-operative using pull requests.
  • Go on a high quality, up-to-date master co-operative.

A strategy that extends these concepts and avoids contradictions will result in a version control workflow for your team that is consistent and like shooting fish in a barrel to follow.

Apply feature branches for your piece of work

Develop your features and gear up bugs in feature branches based off your main branch. These branches are also known as topic branches. Feature branches isolate work in progress from the completed work in the main branch. Git branches are inexpensive to create and maintain. Even modest fixes and changes should accept their ain feature co-operative.

image of basic branching workflow

Creating feature branches for all your changes makes reviewing history elementary. Look at the commits made in the branch and wait at the pull request that merged the branch.

Proper noun your feature branches by convention

Use a consistent naming convention for your feature branches to identify the work done in the branch. Yous tin can also include other information in the co-operative proper noun, such equally who created the co-operative.

Some suggestions for naming your feature branches:

  • users/username/description
  • users/username/workitem
  • bugfix/description
  • feature/feature-name
  • feature/feature-area/feature-name
  • hotfix/clarification

Use feature flags to manage long-running branches

Learn more about using feature flags in your lawmaking.

Review and merge code with pull requests

The review that takes place in a pull request is critical for improving lawmaking quality. Only merge branches through pull requests that pass your review process. Avoid merging branches to the main branch without a pull request.

Reviews in pull requests take time to complete. Your team should agree on what's expected from pull request creators and reviewers. Distribute reviewer responsibilities to share ideas beyond your team and spread out noesis of your codebase.

Some suggestions for successful pull requests:

  • Ii reviewers is an optimal number based on research.
  • If your team already has a code review process, bring pull requests into what y'all're already doing.
  • Take care assigning the same reviewers to a big number of pull requests. Pull requests piece of work better when reviewer responsibilities are shared across the team.
  • Provide plenty detail in the description to quickly bring reviewers upwardly to speed with your changes.
  • Include a build or linked version of your changes running in a staged environment with your pull asking. Others can hands exam the changes.

Go on a loftier quality, upwardly-to-date master co-operative

The code in your main branch should laissez passer tests, build cleanly, and always be current. Your main branch needs these qualities so that feature branches created by your team showtime from a known practiced version of code.

Set up up a branch policy for your main branch that:

  • Requires a pull request to merge lawmaking. This approach prevents direct pushes to the principal branch and ensures word of proposed changes.
  • Automatically adds reviewers when a pull request is created. The added squad members review the code and comment on the changes in the pull request.
  • Requires a successful build to complete a pull request. Code merged into the main branch should build cleanly.

Tip

The build pipeline for your pull requests should be quick to complete, so it doesn't interfere with the review process.

Manage releases

Use release branches to coordinate and stabilize changes in a release of your lawmaking. This co-operative is long-lived and isn't merged back into the principal co-operative in a pull request, different the characteristic branches. Create as many release branches as you need. Keep in listen that each active release branch represents another version of the code y'all demand to support. Lock release branches when you lot're fix to finish supporting a particular release.

Use release branches

Create a release branch from the main branch when you become close to your release or other milestone, such as the end of a sprint. Give this co-operative a clear proper name associating information technology with the release, for example release/xx.

Create branches to fix bugs from the release branch and merge them back into the release branch in a pull request.

image of release branch workflows

Port changes dorsum to the main branch

Make sure that fixes land in both your release branch and your main co-operative. One arroyo is to make fixes in the release branch, then bring changes into your main co-operative to forestall regression in your code. Another approach (and the i employed past the Azure DevOps team) is to always make changes in the mainline, then port those to the release branch. You can read more near our Release Catamenia strategy.

In this topic, we'll cover making changes in the release branch and porting them into mainline. Use reddish-picking instead of merging so that you have verbal control over which commits are ported back to the main branch. Merging the feature branch into the main branch can bring over release-specific changes y'all don't desire in the master branch.

Update the master co-operative with a change fabricated in the release co-operative with these steps:

  1. Create a new feature co-operative off the main co-operative to port the changes.
  2. Cherry-pick the changes from the release branch to your new feature branch.
  3. Merge the feature branch back into the master branch in a second pull request.

Updated release branch workflows.

This release branch workflow keeps the pillars of the basic workflow intact: feature branches, pull requests, and a strong main branch that e'er has the latest version of the code.

Why not apply tags for releases?

Other branching workflows use Git tags to mark a specific commit equally a release. Tags are useful for marking points in your history as of import. Tags innovate actress steps in your workflow that aren't necessary if you're using branches for your releases.

Tags are maintained and pushed separately from your commits. Squad members can easily miss tagging a commit and then take to go back through the history afterwards to fix the tag. Y'all can also forget the extra step to button the tag, leaving the side by side developer working from an older version of the code when supporting the release.

The release branch strategy extends the basic characteristic branch workflow to handle releases. Your team doesn't have to adopt any new version control process other than the ruddy-pick to port changes.

Manage deployments

You can handle multiple deployments of your code in the aforementioned way you lot handle multiple releases. Create a clear naming convention, such every bit deploy/performance-examination, and treat the environment branches like release branches. Your squad should hold on a process to update deployment branches with the code from your main co-operative. Crimson-choice problems fixes in the deployment branch back to the primary co-operative. Apply the aforementioned steps equally porting changes from a release branch.

An exception to this recommendation is if you're using a form of continuous deployment. Utilise Azure Pipelines when working with continuous deployment to promote builds from your principal co-operative to your deployment targets.

Videos