Skip to main content

Command Palette

Search for a command to run...

Mastering Git Branching Strategies for DevOps

Updated
•4 min read•View as Markdown
Mastering Git Branching Strategies for DevOps
A

Aslam Ahemad is a dedicated DevOps Engineer who is passionate about sharing his insights and experiences from working in the IT industry. He is a technophile. Aslam loves exploring new tech, contributing to open-source projects, and managing his team in FIFA08.

Git is one of the most popular version control systems used by developers worldwide. With Git, developers can track changes in code, collaborate with other team members, and safely experiment with different versions of their code.

As a DevOps engineer, one of the main challenges is to efficiently manage the code base in a secure and organized way. With Git, that challenge can be resolved by following a proper branching strategy.

A Git branching strategy is a set of rules that defines how to manage the codebase through different phases of development. A well-defined branching strategy not only helps manage code more efficiently but can also improve productivity, avoid conflicts, and ensure that the codebase is always stable.

Here are some common Git branching strategies that DevOps teams can follow:

Basic branching

This is the simplest branching strategy for small teams or projects. It consists of only two branches; ‘master’, which represents the stable production code and ‘develop’ for the working codebase. The ‘develop’ branch is used to merge code from different team members and make sure that it’s tested before pushing the code to the stable branch.

Feature branching

The feature branching strategy is a more comprehensive branching strategy suitable for bigger teams or larger projects. In feature branching, developers make branches for each new feature they are working on. This helps isolate changes made to that feature and ensures that the main codebase in the “develop” branch remains stable.

# Creating a new feature branch
$ git checkout -b feature/new-feature-branch

# Make changes to the code, commit and push the changes
$ git commit -m "added new feature"
$ git push origin feature/new-feature-branch

# Once feature is complete, merge with develop branch
$ git checkout develop
$ git merge feature/new-feature-branch

# Delete the feature branch
$ git branch -d feature/new-feature-branch

Release branching

The release branching strategy is used to manage releases to production. Developers create a new branch from the stable “master” branch and stabilize the codebase. They use this new branch to make any final changes, fixes, and testing and then merge the code into the “master” branch. This ensures greater stability and helps resolve any issues that may arise during production deployment.

# Creating a new release branch
$ git checkout -b release/release-branch

# Perform any final testing in the release branch
# If any fixes are required, commit and push the changes
$ git commit -m "fix for release"
$ git push origin release/release-branch

# Merge the release branch into the master and develop branches:
$ git checkout master
$ git merge release/release-branch
$ git checkout develop
$ git merge release/release-branch

# Tag the release
$ git tag -a v1.0 -m "Release 1.0"

# Delete the release branch
$ git branch -d release/release-branch

GitFlow branching

The GitFlow branching strategy is a widely used branching strategy for managing large, complex projects. It builds upon the basic branching strategy and expands upon it. It consists of a master branch and two primary branches: ‘develop’ and ‘release’. It also includes two other types of branches: feature and hotfix branches.

The feature branches are used for developing new features, while hotfix branches are used for fixing production issues. The release branch is a branch created from the stable develop branch prior to final staging before production deployment.

# Creating a new feature branch
$ git flow feature start new-feature-branch
# Make Changes and test
$ git commit -m "added new feature"
$ git flow feature publish new-feature-branch

# Once feature is complete, finish the feature branch
$ git flow feature finish new-feature-branch

# Creating a new release branch
$ git flow release start v1.0.0
# Make any final changes, fixes or testing
$ git commit -m "fix for release"
$ git flow release publish v1.0.0

# Once the release is stable, finish the release branch
$ git flow release finish v1.0.0

# Creating a new hotfix branch to fix production issues
$ git flow hotfix start v1.0.1
# Make changes and fix issues
$ git commit -m "fixed critical issue"
$ git flow hotfix publish v1.0.1

# Once the hotfix is complete, finish the hotfix branch
$ git flow hotfix finish v1.0.1

# Merge changes into develop and master branch
$ git checkout develop && git merge --no-ff release/v1.0.0
$ git checkout master && git merge --no-ff release/v1.0.0

# Tag the release
$ git tag -a v1.0.0 -m "Release 1.0.0"

Conclusion

Choosing the right Git branching strategy is crucial for a DevOps team to manage code efficiently and effectively. It’s recommended to choose a strategy that suits your project’s needs and the team's workflows. Whatever strategy you choose, remember that it should be flexible enough to accommodate future changes and responsive to feedback from team members.


More from this blog

Aslam Ahemad

24 posts

Aslam Ahemad is a dedicated DevOps Engineer who is passionate about sharing his insights and experiences from working in the IT industry. He is technophile.