DEV Community

Cover image for Introduction to GitFlow
ljc-dev
ljc-dev

Posted on

Introduction to GitFlow

Working together on a project can get pretty chaotic. That's where GitFlow comes in 😎!

GitFlow is a simple set of branching and merging rules for better collaboration, less errors and code conflicts. It's easy to pick up and adapt to your own team's needs.

In GitFlow, the main branches are:

1. Main branch: production-ready code. This is where the working version of your project is stored.

2. Develop branch: created from the main branch, it is used for integration and testing of new features or improvements.

3. Feature branches: each new feature/change should be developed on a separate feature branch created from the develop branch, and then merged back into it when ready.

git branching example

More complex projects might also include:

4. Release branches: created from the develop branch, they allow for final testing, bug fixes, without affecting ongoing development.

5. Hotfix branches: created from the main branch, to fix critical issues or bugs in production.

HowTo GitFlow branching:

Let's say I've created a new repo with a branch main.

1- create a develop branch and switch to it:

git checkout -b develop
Enter fullscreen mode Exit fullscreen mode

2- push the develop branch to the remote repository (if needed):

git push -u origin develop
Enter fullscreen mode Exit fullscreen mode

3- while in the develop branch, create a feature branch and switch to it:

git checkout -b feature/your-feature-name
Enter fullscreen mode Exit fullscreen mode

4- push the feature branch to the remote repository (if needed):

git push -u origin feature/your-feature-name
Enter fullscreen mode Exit fullscreen mode

⚠ Remember to send your feature branch PRs (pull requests) to the develop branch and not main, and develop branch PRs to the main branch (or a release branch).

Sometimes you might find develop or feature branches being X commits behind main. It happens when people forget to update the development branches after successfully merging PR(s) into main. The develop and feature branches should be kept up-to-date to:

  • reduce the chance of merge conflicts
  • detect integration issues early on
  • and for consistency πŸ‘

So make sure to merge changes from the main branch into your develop and feature branches πŸ˜€!

Common Git branching commands:

  • get a list of all branches:
git branch
Enter fullscreen mode Exit fullscreen mode
  • create a new branch:
git branch your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • switch to an existing branch:
git checkout your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • create a new branch and switch to it:
git checkout -b your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • delete a branch:
git branch -d your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • push a branch to a remote repository:
git push origin your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • cloning a specific branch from a remote repository:
git clone -b your-branch-name your-repository-url
Enter fullscreen mode Exit fullscreen mode

Top comments (0)