
Git for the team, part 2: branching, merge, or rebase strategies and how to resolve conflicts
🌐 Machine-translated from Russian. Show original
In this part:
Why negotiate branches#
When more than one person works in the repository, chaos arises without rules: there is unfinished code in main , no one knows what's currently on sale, and the release turns into a "glue" of a dozen branches on the last night.
The branching strategy answers three questions:
- Where does stable code lie?
- Where is the development of the new task taking place?
- How does the code get to the product?
Three strategies that work#
GitHub Flow — the simplest#
- There is one long-lasting branch of
main— it is always ready to be displayed. - Each problem is a short branch of
feature/...frommain. - Ready → merge request → review → merge into
main→ automatic deployment.
Suitable for web services and teams with CI/CD, where they post frequently.
GitLab Flow — with environments#
The same as GitHub Flow, plus environments or release branches:
main→ automatically to the test bench;production(orrelease/2026.11) → to the product; the code is entered there by merging frommainafter verification.
Suitable when there is acceptance between 'ready' and 'on prod': internal systems, 1C, banking integrations, scheduled display windows.
Trunk-based — for mature teams#
- Everyone commits to
main(trunk) in small portions, branches live for hours, at most a day or two. - Unfinished functions are hidden behind feature flags.
- Quick autotests are mandatory for each commit.
Provides the fastest delivery but requires discipline and good test coverage.
Comparison#
| GitHub Flow | GitLab Flow | Trunk-based | |
|---|---|---|---|
| Long-lived branches | main | main + environments/releases | main |
| Feature branch lifetime | days | days | hours |
| CI is needed | preferably | necessarily | |
| Suitable | web, SaaS, pet projects, | internal systems, acceptance, scheduled releases, | large teams with autotests |
| Risk | "raw" code in main without tests | branch divergence of environments | breaking main with weak tests |
💡 And Git Flow (develop, release, hotfix)? It's a 2010 classic for boxed releases. For web and internal systems, it's usually redundant: lots of branches, lots of merges. If you're not releasing versions supporting multiple releases at once—get GitHub Flow or GitLab Flow.
Recommendation for a small team: GitHub Flow, and if you have a test bench and acceptance — GitLab Flow with the productionbranch.
Branch naming#
| Prefix | Example |
|---|---|
feature/ | feature/142-otchet-prodazhi |
fix/ | fix/151-okruglenie-nds |
hotfix/ | hotfix/kassa-ne-pechataet |
chore/ | chore/update-deps |
The task number in the branch name — GitLab will automatically link the branch and merge request to the task.
Merge or rebase#
Both teams "connect" changes, but in different ways.
Merge — creates a merge commit, history shows how the work actually progressed:
git switch main
git merge feature/142-otchet-prodazhi
Rebase — "transfers" your commits over the fresh main, the history is linear:
git switch feature/142-otchet-prodazhi
git fetch origin
git rebase origin/main
git push --force-with-lease # ветка переписана — нужен «аккуратный» force
| Merge | Rebase | |
|---|---|---|
| History | honest, branched, | linear, easy to read |
| Rewrites commits | no | yes |
| Safe for shared branches | no ** | |
| Conflicts | are resolved once | can recur every commit |
Golden rule: rebase — only your own branch, which no one touches except you. Shared branches (main, production) are never rebased.
--force-with-lease instead of --force: overwrites the branch only if there are no other commits on the server that you are unaware of.
How to Merge in GitLab#
In the project settings (Settings → Merge requests → Merge method):
| Method | Result |
|---|---|
| Merge commit | always commit the merge |
| Merge commit with semi-linear history | merge commit, but only if the branch is updated from main |
| Fast-forward merge | linear history without merge commits |
Plus the Squash commits checkbox — all branch's commits will be compressed into one. Convenient: in the branch, you can commit "wip," "fixed," and main will receive one neat commit headed merge request.
Recommendation: squash + semi-linear or fast-forward — history main read as a list of completed tasks.
Interactive rebase — tidy up the review#
git rebase -i origin/main
In the editor: pick — keep, squash/fixup — merge with the previous one, reword — change message, drop — delete. Do this before submitting the thread to the review.
Conflicts: what they are and how not to be afraid#
A conflict is when two branches have changed the same strings differently, and Git cannot resolve it itself. This is normal, not a crash.
git merge main # или git rebase origin/main
# CONFLICT (content): Merge conflict in src/discount.ts
git status # список конфликтных файлов
Tags will appear in the file:
<<<<<<< HEAD
const MAX_DISCOUNT = 30;
=======
const MAX_DISCOUNT = 25;
>>>>>>> feature/151-skidka
Procedure:
- Open the file, decide which option is correct (or combine both). If unsure, ask the author of the second change.
- Remove the tags
<<<<<<<,=======,>>>>>>>. - Run the tests.
- Complete:
git add src/discount.ts
git commit # при merge
git rebase --continue # при rebase
They changed their minds — git merge --abort or git rebase --abort will return everything to how it was.
It is more convenient to resolve conflicts in the IDE: VS Code, JetBrains, and 1C:EDT show three panels — "your," "their," and "result." Simple conflicts can be resolved directly in the GitLab web interface on the merge request page.
How conflicts decrease#
- Short branches. A branch that lives for a week is almost guaranteed to conflict.
- Pull the main into your thread more often (
git rebase origin/maintimes a day). - Small merge requests — up to 300–400 lines.
- Autoformatting (Prettier, Black, gofmt) — otherwise, conflicts arise due to spaces.
- Do not mix refactoring and new functionality in the same branch.
git config --global rerere.enabled true— Git will remember how you resolved the conflict and repeat it on the next rebase.
Part 2 checklist#
- Selected and recorded in the README strategy: GitHub Flow, GitLab Flow, or trunk-based
- We agreed on branch names with task numbers
- Rebase — only its own branches, push — from
--force-with-lease - Squash is enabled in GitLab and the merge method is chosen
- Branches live no longer than a few days
Next — how to organize code review and protect main: Git for the team, part 3: code review and merge request — rules, templates, CODEOWNERS, and protected branches.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.