Level-up Your Git Projects with Gitflow

dedsyn4ps3

Ed Rutherford

Posted on June 6, 2024

Level-up Your Git Projects with Gitflow

Take your project’s design and evolution to the next level by integrating Gitflow into your development process!

Info

In the Beginning

I’m fairly certain that many of you reading this are plenty familiar with the fantastic ecosystem that is Git, as well as some of the gigantic version control platforms such as Github and GitLab. That being said, there could still be some newbie programmers reading this article, and may need a quick primer on what Git is…so here’s your brief explanation:

Git is a version control system that allows you to manage and keep track of your source code history. It’s like a time machine for your code, enabling you to save snapshots of your changes and collaborate with others. GitHub, on the other hand, is a cloud-based hosting service specifically designed for managing Git repositories. It provides a platform where developers can store, create, manage, and collaborate on code.

As I’m sure many of you can relate, getting new projects up and running can be a challenge…and I’m just talking about drafting out and writing the code! Like a lot of us come to find out, managing a codebase can be quite a bit more involved than one might think:

  • First, you initialize your new repo and link it to a remote branch (if doing it locally)
  • Then, you start writing some code…easy right?
  • Next thing you know, your project is growing, new ideas emerge to add to the project…and of course, the occasional bug or hot-fix that may be necessary!
  • Finally, you keep making changes bit by bit, then commit and push the changes to your remote, with the occasional tag here and there…then repeat…

If the codebase isn’t all that large or complex, following such a straightforward development approach is more than likely going to be okay. But what happens if you’re creative juices are flowing and your once-small project now becomes more of a thriving repo with the potential for other collaborators to get involved, or perhaps you begin planning on packaging and distributing the code as it becomes more developed?

Enter Workflows

Lucky for us, there are several solutions for dealing with projects that grow in scope and complexity! We'll be focusing primarily on one of these solutions, which has become a key piece in my own improved development life-cycle (as well as countless other developers). Before we dive into it, let's have a quick introduction into what all we're talking about here...

Flow

Some of you may or may not be familiar with the term workflow when it comes to project development strategy. I'm sure it rings a bell for many of you, whether you know exactly what it is or not:

A workflow is an end-to-end process that...move[s] data (tasks) through a series of steps from initiation to completion. Once it’s set up, a workflow helps you organize information in a way that is not only understandable, but also repeatable.1

When it comes to Git project development, there are three popular workflows that are typically implemented by developers and teams:

  • Centralized
  • Feature Branch
  • Gitflow

Each of these has their own pros and cons, and for many are reliable ways to effectively build and maintain projects. Many of you will more than likely identify with using at least one of these workflows in your day to day coding experiences, even if you never knew it!

  1. Centralized Workflow:
    • Uses a central repository (often named main) as the single point of entry for all changes.
    • Developers commit directly to the main branch.
    • Advantages: Simple and easy to understand.
  2. Feature Branch Workflow:
    • Developers create separate branches for individual features or bug fixes.
    • Each feature branch is isolated and independent.
    • After development, feature branches are merged back into the main branch.
    • Advantages: Branch isolation as well as parallel development ability.
  3. Gitflow Workflow:
    • Extends the feature branch workflow.
    • Introduces additional branches: develop (for ongoing development) and release (for release candidates).
    • Feature branches are created from develop (or dev), and hotfix branches are based on main.
    • Advantages: Clear structure, well-defined roles for branches, and support for both features and releases.

Implementing Gitflow

How many of you immediately identified with a particular workflow after reading the previous section? I have a feeling many of you relate to using the first style of workflow, which is totally fine! What I hope to do here is expand your understanding of other common flows, in the event that your projects grow or you simply want a more organized structure for all your code.

Like many developers, I too was consistently using a Centralized Workflow for quite a long time. Once my projects started becoming significantly more complex, it became clear that a new flow had to be implemented...and the flow that worked best (and continues to work great) for these projects was Gitflow!

Getting started is pretty straightforward and painless for most, especially if you have full control of your git repo (which from this point on, I will assume you have). There are only a handful of required actions that need taken when setting the flow up, many of which can be done automatically depending on the code management platform you're using.

Enable Flow

As you can see in the above image, desktop Git applications such as GitKraken offer built-in Gitflow integration, making it extremely easy to implement Gitflow in your projects!

My personal preference is to use GitKraken for all my desktop-based code management, though you're free to utilize whichever platform works well for you...

Important

If for some reason your desktop Git client doesn’t have the option to enable Gitflow, or perhaps you prefer sticking with the terminal…have no fear! There's a dedicated git-flow extension available to install using your system's package manager!

Once installed, you can just as easily enable the flow in an existing repository or when creating a new one...

# For Debian/Ubuntu Distros
sudo apt install -y git-flow

# For Archlinux
yay -S gitflow-avh

# Using Homebrew
brew install git-flow
Enter fullscreen mode Exit fullscreen mode

Putting it Together

Once we have either enabled Gitflow functionality in our desktop Git client, or installed it locally using our system’s package manager, it’s time to actually put it to use!

Using a desktop client such as GitKraken works out to be the simplest option, since all we need to do is enable it in our repository’s settings tab, and define what we want the various branches to be named. Once that’s done, we’re off to the races!

When following the manual approach to things, we first need to initialize the flow in our repo and define what our various branches should be called using the git-flow CLI:

# Enable Gitflow in our project
> git flow init
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master] main
Branch name for "next release" development: [develop] dev

How to name your supporting branch prefixes?
Feature branches? [feature/] feat
Release branches? [release/] 
Hotfix branches? [hotfix/] fix
Support branches? [support/] 
Version tag prefix? []
Enter fullscreen mode Exit fullscreen mode

Now that our new flow is enabled, we can go about developing our code as we normally would…for the most part! Remember, the whole purpose of implementing a Gitflow-styled development process is to better compartmentalize the various stages of the entire process:

  • The core, stable codebase will always follow the main branch (or whatever you named it)
  • Use the dev branch to make and push changes as you test and grow your project
  • When something doesn’t work right after a dev commit, use the hotfix type branch to make corrections and merge into dev
  • After accomplishing a milestone (i.e. finished a new component or code section), merge the current dev branch into main

Any Thoughts?

Some of you may be wondering to yourselves: “What’s the point of doing all this?”. I’m glad you asked! While it may seem like a bit of extra work to enable and implement different branches in a project, doing so will help keep your code commits organized and overall project more structured than a typical single-branch style repo.

By maintaining such structure in your projects, you’ll without a doubt become a better developer that’s much better equipped to work on larger projects down the road…be they your own or even an internal corporate project!

Current and potential employers alike typically appreciate seeing such well-structured code and commit histories that models like Gitflow produce…

Commit Graph

Get to it!

Now that you have a better understanding of what Gitflow is, and how it can help you become a better developer, get out there and try it out! The important thing to remember is that while not all projects need implement such a flow…many of them can and should.

Pick a project that you’ve been working on (or even start a new one), and follow the steps that apply to you to enable Gitflow. Once you do, see what kind of changes you experience while following this workflow!

Do you find yourself better organizing your commits and code merges as your project grows in complexity? Perhaps you don’t think it makes much of a difference for the project you’re currently working on? It can happen sometimes!

Thanks!

1) Martins, Julia. “Hitting Work Blocks? Try Workflows. [2024].” Asana, Asana, 17 Jan. 2024, asana.com/resources/workflow-examples.


💖 💪 🙅 🚩
dedsyn4ps3
Ed Rutherford

Posted on June 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related