Module 1 · Beginner · DevOps foundations

Git branching workflow for teams

A Git branching workflow for teams: feature branches, pull requests, code review, merge vs rebase vs squash, resolving conflicts, and trunk-based development.

By theprogrammers team · · 7 min read

When you code alone, you can commit straight to main and nobody minds. In a team, that quickly turns into chaos: half-finished features break the app, two people overwrite each other's work, and nobody knows which version is on the server. A branching workflow is the set of rules a team agrees on for how changes move from someone's laptop into the main code, and then into production.

This lesson explains the workflow most teams use today, feature branches and pull requests, step by step with the exact Git commands. It also covers merging strategies, conflicts, and how this connects to CI/CD. It assumes you know basic Git (add, commit, push).

Branches in one paragraph

A branch is a separate line of work. Creating one is like making a copy of the project where you can change things freely, without affecting anyone else. When your change is ready, you merge it back. main is the shared branch that everyone builds on, and in most teams it should always work.

The feature branch workflow

This is the standard workflow, used with GitHub, GitLab and Bitbucket:

  1. Start from an up-to-date main.
  2. Create a short-lived branch for one change.
  3. Commit your work on that branch.
  4. Push it and open a pull request (PR; GitLab calls it a merge request).
  5. Automated checks (CI) run, and a teammate reviews the code.
  6. Fix any problems, then merge into main.
  7. Delete the branch. main is deployed.

Here are the commands.

1 and 2: start a branch

git switch main
git pull                          # get everyone else's latest work
git switch -c feature/login-otp   # create a branch and switch to it

Name branches so their purpose is clear. A common pattern is type/short-description:

PrefixFor
feature/New functionality: feature/login-otp
fix/Bug fixes: fix/cart-total-rounding
docs/Documentation only
chore/Maintenance, such as updating dependencies

3: commit in small steps

git add src/otp.py tests/test_otp.py
git commit -m "Add OTP generation and expiry check"

Good commit messages say what changed and, if it isn't obvious, why, in the imperative mood ("Add", "Fix", not "Added"). Small commits are easier to review and to undo.

4: push and open a pull request

git push -u origin feature/login-otp

-u links your local branch to the remote one, so later you can just type git push. GitHub then shows a button to open a pull request. In the description, say what changed, why, and how you tested it, and link the issue it fixes.

5: checks and review

The PR is where quality happens:

  • CI runs automatically: tests, linting and a build, as shown in run tests and linters automatically in CI. A red cross means something broke.
  • A teammate reviews the code: Is it correct? Is it readable? Are there tests? Is anything risky?
  • Branch protection on main can make both required: no merging until checks pass and someone approves.

When reviewing, be kind and specific ("this loop runs once per order; could we fetch all orders in one query?"), and when receiving reviews, remember the comments are about the code, not you.

6: keep your branch up to date

If main moved on while you worked, bring those changes into your branch before merging:

git switch main
git pull
git switch feature/login-otp
git merge main          # or: git rebase main

7: merge and clean up

Merge with the button on GitHub, then tidy up locally:

git switch main
git pull
git branch -d feature/login-otp

Merge, squash or rebase?

GitHub offers three ways to merge a PR. They produce the same code, but different history:

MethodWhat happensHistory looks like
Merge commitKeeps every commit, plus a commit joining the branchesComplete but busy, with branch lines
Squash and mergeCombines the PR's commits into one commit on mainOne clean commit per PR
Rebase and mergeReplays each commit on top of main, no merge commitA straight line of every commit

Many teams choose squash and merge: each PR becomes one commit, so main's history reads like a list of changes, and undoing a feature is one git revert.

A note on rebase: it rewrites commits. That's fine on your own branch, but never rebase a branch other people are working on. After rebasing an already-pushed branch, you need git push --force-with-lease, which refuses to overwrite work you haven't seen.

Resolving merge conflicts

A conflict happens when two branches changed the same lines. Git can't guess which is right, so it marks the file:

<<<<<<< HEAD
OTP_EXPIRY_SECONDS = 300
=======
OTP_EXPIRY_SECONDS = 120
>>>>>>> feature/login-otp

The part above ======= is the current branch's version; below it is the incoming change. To resolve it:

  1. Edit the file to the correct final version, and delete the three marker lines.
  2. git add the file.
  3. Finish with git commit (for a merge) or git rebase --continue (for a rebase).

Run the tests afterwards: code can merge without conflicts and still be wrong together. Small, short-lived branches are the best prevention, because they have less time to drift apart.

Other workflows you'll hear about

WorkflowHow it worksSuits
GitHub flowmain plus short feature branches and PRs; main is always deployableMost web apps and teams
Trunk-based developmentEveryone merges small changes into main at least daily; unfinished features hidden behind feature flagsTeams with strong CI that deploy many times a day
Git flowLong-lived develop, release/ and hotfix/ branches alongside mainSoftware with scheduled versioned releases, like mobile or desktop apps

Git flow was very popular, but for web apps with CI/CD, its long-lived branches cause big, painful merges. Most teams today use GitHub flow or trunk-based development. A feature flag is an on/off switch in the code that lets you merge a half-built feature safely while keeping it hidden from users.

Habits that keep a team's Git healthy

  • Keep branches short-lived: hours or a few days, not weeks.
  • Keep PRs small: under about 400 lines changed gets much better reviews.
  • Pull main often to catch conflicts early.
  • Never commit secrets. Add .env to .gitignore. If a secret is committed, treat it as leaked and change it: deleting the file later doesn't remove it from history.
  • Protect main: require PRs, passing checks and a review.
  • Tag releases: git tag v1.4.0 marks exactly what was deployed.

Practice

  1. Write the commands to start work on a bug fix for wrong GST calculation, from an up-to-date main.
Show the answer
git switch main
git pull
git switch -c fix/gst-calculation
  1. Your PR shows "This branch has conflicts that must be resolved". What do you do?
Show the answer

Update main locally, merge (or rebase) it into your branch, edit the conflicting files to the correct version and remove the markers, git add them, commit (or git rebase --continue), run the tests, and push.

  1. You accidentally committed an API key and pushed it. What should you do?
Show the answer

Revoke or rotate the key immediately, since it must be treated as leaked. Then remove it from the code, store it as a secret instead, and optionally clean the history with a tool such as git filter-repo.

Interview questions

Describe the Git workflow you'd use in a team.

Feature branches off main, small commits, a pull request per change with CI checks and code review, branch protection on main, squash-merging, then automatic deployment from main. Keep branches short-lived and main always deployable.

What is the difference between git merge and git rebase?

Merge joins two histories with a merge commit, preserving exactly what happened. Rebase replays your commits on top of another branch, creating a linear history but rewriting commit IDs, so it shouldn't be used on shared branches.

What is trunk-based development?

A workflow where developers integrate small changes into the main branch at least daily, relying on strong automated testing and feature flags instead of long-lived branches.

How do you resolve a merge conflict?

Open the conflicting files, choose or combine the changes between the conflict markers, remove the markers, stage the files, complete the merge or rebase, and run the tests.

What to learn next

  1. Learn the format pipelines are written in: YAML for beginners.
  2. Make every pull request run checks in run tests and linters automatically in CI.