Professional Git Workflow & GitHub Setup for (React) Developers (incl screencast)
Tags: git, github, react, webdev
Published: 2022-03-18 12:31:58

---
title: Professional Git Workflow & GitHub Setup for (React) Developers (incl screencast)
published: true
description: Solo devs work on the main branch. Teams don’t. They use professional workflows like Trunk-Based Development. This guide shows how you can too.
tags: git, github, react, webdev
canonical_url: https://profy.dev/article/trunk-based-development-with-github
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5e1wfw5edolh280gpgj.png
series: Professional React Development
---

If you’re a solo developer working on your own projects your Git workflow is usually simple: You work on the main (or master) branch all day every day.

You probably know that professional developer teams don't work like this. Multiple devs all committing to the main branch can quickly become chaotic. And it’s likely that unreviewed or untested code makes it into production eventually. Danger!

Professional teams use processes and workflows to prevent this from happening. And the most common Git workflow used in developer teams (at least from my experience):

[Trunk-Based Development](https://trunkbaseddevelopment.com/#scaled-trunk-based-development) (or more correctly Scaled Trunk-Based Development).

If your goal is to find a job as a professional developer I highly recommend getting used to this workflow upfront. The more you know how to work like a professional the less you’ll be overwhelmed on your first job.

I promise: it’s not very difficult if you know the basics of Git. But there is a lot of glossary around it that might confuse you at first.

Watch the video below to see me walk through one cycle of the workflow. Below the video you can find instruction on how to set up branch protection in your GitHub repository to enforce this workflow and a detailed walkthrough based on screenshots.

> This series of articles and the application I'm building are part of my [upcoming React Job Simulator](https://profy.dev/waitlist?utm_source=tbd-2&utm_medium=devto). Build a project hands-on like a professional team and gain job-like experience.

{% embed https://www.youtube.com/watch?v=rIcHdVcuPzw %}

## Table of Contents

1. [Trunk-Based Development in a Nutshell](#trunkbased-development-in-a-nutshell)
2. [Branch Protection: Enforce usage of Pull Requests](#branch-protection-enforce-usage-of-pull-requests)
3. [A tour through the Trunk-Based Development workflow](#a-tour-through-the-trunkbased-development-workflow)
    1. [Open a Pull Request](#open-a-pull-request)
    2. [Continuous Integration Pipeline](#continuous-integration-pipeline)
    3. [Code Reviews](#code-reviews)
    4. [Handling Review Comments](#handling-review-comments)
    5. [Approving a Pull Request](#approving-a-pull-request)
    6. [Merging the Pull Request](#merging-the-pull-request)
    7. [The history of the main branch](#the-history-of-the-main branch)
    8. [Updating the local main branch](#updating-the-local-main-branch)
4. [Summary](#summary)

## Trunk-Based Development in a Nutshell

- You check out a new branch from the main branch.
- You commit your code on this branch and push it to the GitHub repo.
- You open a Pull Request (or Merge Request as GitLab calls it).
- Automated tests verify that the application behaves as expected.
- A teammate reviews your code and you adjust it according to the feedback.
- You merge your branch into the main branch via the Pull Request (short PR).

> If this is all sounds like Gibberish, no worries. Have a look at [my free course where you can learn and practice this workflow](https://profy.dev/project/github-minesweeper).
> 

## What the heck is a Pull Request?

I always found the name Pull Request on GitHub confusing. GitLab calls it a Merge Request which is much more descriptive. Basically, a Pull Request is a way to ask for permission to merge your code into the main branch:

“*Hey team, can somebody have a look at this code and tell me if it’s any good? I’d like to get it into the main branch so our users can benefit from it.”*

You can think of a Pull Request as a feature on top of a Git branch that allows you to get feedback from your teammates. And as mentioned it also allows you to automatically run checks and tests on your code changes before they go to the main branch.

To summarize, Pull Requests are

- a mechanism to gather feedback and thus increase code quality
- a tool to run automation (e.g. tests) on your code to decrease the risk of introducing bugs into production code.

## Branch Protection: Enforce usage of Pull Requests

Processes and workflows are great. But people are lazy and look for workarounds. So ideally we want to force everyone on the team to use Pull Requests instead of committing directly to the main branch.

Luckily GitHub has our back with a feature called “branch protection”. To protect the main branch open your repository’s settings on GitHub, select “Branches” in the left menu, and click on the “Add rule” button.

![GitHub add branch protection rule](https://media.graphcms.com/JNUZioz4SIGzkLGuywX8)

![GitHub branch protection](https://media.graphcms.com/a7wRFUTTO2I6Mkvhkeuf)

A few notes about the selected rules:

- In a team of developers the option “Require a Pull Request before merge” → “Require approvals” is mostly activated. This way we can enforce that developers review and approve each other’s code. That is one safeguard against new bugs and ideally increases code quality and coherence.
- The option “Require linear history” is not necessary but from my experience many teams use it nowadays. It prevents merge commits on the respective branch. Instead you have to either “Squash and merge” a Pull Request or “Rebase” it. [You can see the “Squash and merge” in action including an explanation here in the video.](https://youtu.be/QH9rhtp_TMk?t=2373)
- The “Include administrators” option is important if you want to enforce the workflow for yourself in your own repositories. Since you’re the administrator the rules wouldn’t apply to you otherwise.

If a developer now creates a commit on the main branch and tries to push it they will see an error message.

![Push to main branch fails](https://media.graphcms.com/n6XnSXxMQa61x8zwKK0J)

## A tour through the Trunk-Based Development workflow

### Open a Pull Request

```tsx
git checkout -b migrate-to-styled-components
```

Now we write our code, commit and push it to the remote repository on GitHub.

```tsx
git commit -m "Migrate home page to styled-components"
git push origin migrate-to-styled-components
```

On GitHub, you should now see a banner to create a Pull Request.

![Create Pull Request banner on GitHub](https://media.graphcms.com/0sbFsvGOQFmmAsu5hc87)

Once you click the button you see a form where you can enter a title and description. Next click the “Create Pull Request” button.

![Create Pull Request form on GitHub](https://media.graphcms.com/MquYOS3lQEOlPiAox9iQ)

Congrats, you opened your first Pull Request! This is what you should see now:

![Pull Request on GitHub](https://media.graphcms.com/ZNZJn0tQcEGdnA0RfFig)

### Continuous Integration Pipeline

Did you note the status checks at the bottom of the PR?

![Status check for Continuous Integration Pipeline](https://media.graphcms.com/lHT8n4Q3SVGiv6IhrF9Z)

This is a really handy feature. You can run scripts like `npm run lint` or `npm run test` within your Pull Requests to decrease the risk of introducing bugs. This is called a Continuous Integration pipeline. I’ll leave it as a cliffhanger for tomorrow’s article. If you can’t wait you can already watch me set it up in the video.

### Code Reviews

In a real-world team, your code is typically reviewed by at least one teammate. This again prevents bugs and helps keep the codebase clean and consistent. A Pull Request is also a great way of discussing your code in case you’re stuck.

So let’s switch to another account with access to the repository. Here is how our imaginary teammate would review your code.

![Pull Request - files changed](https://media.graphcms.com/wxuyaCsLQB63b4koeNPF)

Our teammate can add comments to the code.

![Pull Request - review comments](https://media.graphcms.com/KCkROL6QOeQvvV7kFoUt)

Finally, they submit the review.

![Pull Request - Submit review](https://media.graphcms.com/o3v2TE68TVWTGTxciejV)

As the author of the Pull Request, we can now see the comments.

![Pull Request - Review from author's perspective](https://media.graphcms.com/PXHYoGfSCuf6ivFrcVzw)

### Handling Review Comments

We have two options now: we can update our code according to the comments or start a discussion.

To adjust our code we simply head back to our local machine, change the code, commit and push it. You can see the new commit below the review comments. You can also add a comment and resolve the conversation.

![Pull Request - Resolve comment](https://media.graphcms.com/61gWRYpCQ820a7cth2Cf)

Finally, you can request a new review:

![Pull Request - Request new review](https://media.graphcms.com/kqWN2AEQE6EbIiFnXOo9)

### Approving a Pull Request

Once your teammate is pleased they can approve your Pull Request by submitting a review. They might add a useless yet affirmative emoji comment to make you happy.

![Pull Request - Approve](https://media.graphcms.com/6ryF7cPbQKWutMFQpKzF)

### Merging the Pull Request

Finally, it’s time to merge our Pull Request. Now our code changes will be added to the main branch.

![Pull Request - Approved](https://media.graphcms.com/VrGSvIPQ0GICNQN1RIKg)

Remember that we set the “Require linear history” option in our branch protection rules? That’s why we see a “Squash and merge” button instead of a simple “Merge” button by default.

Let’s see what happens when we push the button:

![Pull Request - Squash and Merge](https://media.graphcms.com/85jO01xT3KdqG1wFoWiV)

And once we press the confirmation button we’re all set.

![Pull Request - Squashed and merged](https://media.graphcms.com/DGaaX9YS32Ylv1eqSx5A)

### The history of the main branch

I didn’t explain yet what the “Squash and merge” button does, right? The Pull Request (or our Git branch) contained multiple commits:

![Commits in pull request](https://media.graphcms.com/U4r3wQRASRakvc6Zgx8b)

When we look at the commit history of our main branch we don’t see these commits anymore. Instead, there’s only a single commit that points to the Pull Request (via the link `#6`):

![Squashed commit in main branch](https://media.graphcms.com/nygmdDZLSUynzgcQP3xY)

All the commits of our original branch have been squashed into a single commit. The benefit here is that you don’t need to keep the commits in the Pull Request super tidy. For example, commits that are simple fixes during the review process like “Remove unused class name” don’t really need to show up in the history of the main branch.

### Updating the local main branch

As the last step (that’s easy to forget) we sync our local main branch with the remote repository. Since the merge happened on GitHub our local machine doesn’t know about these changes in the main branch yet.

```tsx
git pull origin main
```

When we work in a team of developers we should actually do this every time we start working on a new branch.

## Summary

In this article you learned how to set up a GitHub repository with branch protection to enforce a popular Git workflow called Trunk-Based Development. By now, I hope you're less intimidated by Git & GitHub thanks to the detailed walkthrough.

[![React Job Simulator waitlist](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/907k6850wi7tcskg9bo5.png)](https://profy.dev/waitlist?utm_source=tbd-2&utm_medium=devto)