Skip to content

Git Workflow Tutorial

Overview

Git workflows define the processes and best practices for using Git to manage code changes and collaborate with others. Understanding and using the right workflow can improve your team's efficiency and ensure a smooth development process. This document covers several common Git workflows with examples.

Centralized Workflow

The Centralized Workflow is a simple workflow where all developers work directly on a single branch, usually main or master. This is similar to traditional version control systems.

Workflow Steps

  1. Clone the Repository:

    git clone <repository-url>
    
  2. Make Changes:

    Edit files and make changes as needed.

  3. Add and Commit Changes:

    git add <file>
    git commit -m "Describe your changes"
    
  4. Push Changes:

    git push origin main
    

Example

Developers commit and push their changes directly to the main branch. This workflow is suitable for small teams or projects with a simple development process.

Feature Branch Workflow

The Feature Branch Workflow involves creating separate branches for each new feature or bug fix. This helps keep the main branch clean and stable.

Workflow Steps

  1. Create a Feature Branch:

    git checkout -b feature-branch
    
  2. Make Changes and Commit:

    git add <file>
    git commit -m "Add new feature"
    
  3. Push Feature Branch:

    git push origin feature-branch
    
  4. Create a Pull Request (PR):

    Create a PR from feature-branch to main on your Git hosting service.

  5. Merge PR and Delete Branch:

    Once approved, merge the PR and delete the feature branch:

    git branch -d feature-branch
    git push origin --delete feature-branch
    

Example

Developers work on separate branches for new features. Once completed, branches are merged into main after code review.

Gitflow Workflow

Gitflow is a popular branching model that defines a strict branching strategy with multiple types of branches for different purposes.

Workflow Steps

  1. Initialize Gitflow:

    git flow init
    
  2. Create a Feature Branch:

    git flow feature start <feature-name>
    
  3. Finish a Feature:

    git flow feature finish <feature-name>
    
  4. Create a Release Branch:

    git flow release start <release-version>
    
  5. Finish a Release:

    git flow release finish <release-version>
    
  6. Create a Hotfix Branch:

    git flow hotfix start <hotfix-version>
    
  7. Finish a Hotfix:

    git flow hotfix finish <hotfix-version>
    

Example

Gitflow is suitable for projects with planned releases and hotfixes. It uses branches for features, releases, and hotfixes.

Forking Workflow

The Forking Workflow is used in open-source projects where contributors fork the repository, make changes in their fork, and then submit pull requests.

Workflow Steps

  1. Fork the Repository:

    Use the fork feature on your Git hosting service to create a personal copy.

  2. Clone Your Fork:

    git clone <your-fork-url>
    
  3. Create a Feature Branch:

    git checkout -b feature-branch
    
  4. Make Changes and Commit:

    git add <file>
    git commit -m "Add new feature"
    
  5. Push Changes to Fork:

    git push origin feature-branch
    
  6. Create a Pull Request:

    Create a PR from feature-branch in your fork to main in the original repository.

Example

Contributors fork the repository, work on their changes in separate branches, and create pull requests to merge changes into the main repository.

GitHub Flow

GitHub Flow is a simplified workflow for projects that deploy regularly. It focuses on using feature branches and pull requests.

Workflow Steps

  1. Create a Feature Branch:

    git checkout -b feature-branch
    
  2. Make Changes and Commit:

    git add <file>
    git commit -m "Add new feature"
    
  3. Push Changes:

    git push origin feature-branch
    
  4. Create a Pull Request:

    Create a PR from feature-branch to main on GitHub.

  5. Merge PR and Deploy:

    Merge the PR once approved, and deploy the changes.

Example

GitHub Flow is ideal for continuous deployment environments where changes are pushed and deployed frequently.

GitLab Flow

GitLab Flow is a flexible workflow that combines aspects of Gitflow and GitHub Flow. It supports various branching strategies and integrates with CI/CD pipelines.

Workflow Steps

  1. Create a Feature Branch:

    git checkout -b feature-branch
    
  2. Make Changes and Commit:

    git add <file>
    git commit -m "Add new feature"
    
  3. Push Changes:

    git push origin feature-branch
    
  4. Create a Merge Request (MR):

    Create an MR from feature-branch to main on GitLab.

  5. Merge MR and Deploy:

    Merge the MR once approved, and deploy the changes.

Example

GitLab Flow provides flexibility in branching strategies and integrates well with GitLab's CI/CD pipelines for automated testing and deployment.

Choosing the Right Workflow

The right Git workflow depends on your project’s needs and team structure. Consider factors such as team size, release strategy, and deployment frequency when choosing a workflow. Common workflows include:

  • Centralized Workflow: For simple projects or small teams.
  • Feature Branch Workflow: For projects with multiple features and releases.
  • Gitflow Workflow: For projects with a structured release and hotfix strategy.
  • Forking Workflow: For open-source projects and large contributor bases.
  • GitHub Flow: For continuous deployment and frequent changes.
  • GitLab Flow: For flexible workflows and integration with CI/CD pipelines.

Summary

Git workflows define how teams manage code changes and collaborate using Git. Understanding and implementing the right workflow can streamline development processes, improve code quality, and enhance team productivity. Whether you use a simple centralized workflow or a more complex strategy like Gitflow or GitLab Flow, choosing the right approach is crucial for effective version control.