Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Git Workflows

Different types of Git workflows

Git workflows define how teams use Git to manage source code and collaborate on projects. There are several established Git workflows, each suited for different team sizes and project requirements. This guide explores the different types of Git workflows.

Key Points:

  • Workflows define the branching and merging strategies used in a project.
  • Choosing the right workflow depends on your team's size, project complexity, and collaboration style.
  • Common workflows include Centralized Workflow, Feature Branch Workflow, Gitflow Workflow, Forking Workflow, and Trunk-Based Development.

Centralized Workflow

The Centralized Workflow uses a central repository where all changes are committed. It is simple and easy to understand, making it suitable for small teams or projects with less complexity.


# Clone the central repository
$ git clone https://github.com/username/repo.git

# Create a feature branch
$ git checkout -b feature-branch

# Make changes and commit
$ git add .
$ git commit -m "Add feature"

# Push changes to the central repository
$ git push origin feature-branch

# Merge changes into the main branch
$ git checkout main
$ git merge feature-branch
$ git push origin main
                

Feature Branch Workflow

The Feature Branch Workflow involves creating a new branch for each feature or bug fix. This workflow allows multiple developers to work on different features simultaneously without interfering with each other's work.


# Clone the repository
$ git clone https://github.com/username/repo.git

# Create a new feature branch
$ git checkout -b feature-branch

# Make changes and commit
$ git add .
$ git commit -m "Add new feature"

# Push the feature branch to the remote repository
$ git push origin feature-branch

# Create a pull request to merge the feature branch into the main branch
                

Gitflow Workflow

The Gitflow Workflow defines a strict branching model designed around the project release. It uses two main branches, main and develop, and several supporting branches for features, releases, and hotfixes.


# Clone the repository
$ git clone https://github.com/username/repo.git

# Create a develop branch from the main branch
$ git checkout -b develop

# Create a feature branch from the develop branch
$ git checkout -b feature-branch develop

# Make changes and commit
$ git add .
$ git commit -m "Add new feature"

# Merge the feature branch back into the develop branch
$ git checkout develop
$ git merge feature-branch

# Create a release branch from the develop branch
$ git checkout -b release-1.0 develop

# Finalize the release and merge into main
$ git checkout main
$ git merge release-1.0

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

# Merge the release branch back into develop
$ git checkout develop
$ git merge release-1.0

# Push all branches and tags
$ git push origin --all
$ git push origin --tags
                

Forking Workflow

The Forking Workflow is commonly used in open-source projects. Each contributor forks the main repository, makes changes in their forked repository, and creates a pull request to merge changes back into the main repository.


# Fork the repository on GitHub

# Clone your forked repository
$ git clone https://github.com/your-username/repo.git

# Add the original repository as a remote
$ git remote add upstream https://github.com/original-username/repo.git

# Create a new branch for your changes
$ git checkout -b feature-branch

# Make changes and commit
$ git add .
$ git commit -m "Add new feature"

# Push the changes to your forked repository
$ git push origin feature-branch

# Create a pull request to merge your changes into the original repository
                

Trunk-Based Development

Trunk-Based Development involves keeping a single branch, typically the main branch, and developing features directly on this branch. Developers commit small, frequent changes to the main branch and rely on feature toggles to manage incomplete features.


# Clone the repository
$ git clone https://github.com/username/repo.git

# Create a new branch for your changes
$ git checkout -b feature-branch

# Make changes and commit
$ git add .
$ git commit -m "Add new feature"

# Rebase your branch onto the latest main
$ git pull --rebase origin main

# Merge changes into the main branch
$ git checkout main
$ git merge feature-branch

# Push changes to the remote repository
$ git push origin main
                

Summary

This guide covered different types of Git workflows, including Centralized Workflow, Feature Branch Workflow, Gitflow Workflow, Forking Workflow, and Trunk-Based Development. Choosing the right workflow depends on your team's needs, project complexity, and collaboration style.