SteelReed

Search

Search posts, questions…

1. Java Core

Git in a team: branches, merge or rebase, conflicts, the reflog and pull requests

Why long-lived branches breed conflicts and why rewriting shared history breaks other people's work: fast-forward, resolving conflicts, merge or rebase, revert or reset, Git Flow or trunk-based, the reflog, bisect, pull requests, and how to check a conflict an AI resolved.

17 Nov 20268 min readMid

–

1. The problem

Two developers change totalPrice on two branches that live for 3 weeks. The merge conflicts in 12 files; the resolution removes every marker and the build is green, but prices no longer subtract the discount. The next day someone runs reset --hard then push --force on the release branch, wiping out 2 commits from a colleague.

2. Key idea

Two sources of trouble: divergence between branches (the longer they live, the more edits overlap) and rewriting shared history (others are building on the old hashes). Git compares lines, so tests are the referee after every merge.

Before each command, ask two questions: has anyone built on these commits yet, and how long will this branch live.

Operation Use when Don't use when
git rebase main your own feature branch, nobody built on it a branch others have pulled
git merge merging into a shared branch, keeping "when the feature landed" just updating your own branch
Squash and merge PRs full of "wip" commits you need every commit for bisect
git revert undoing on main, release/* commits not yet pushed
git push --force-with-lease pushing your own branch after a rebase shared branches

2.1 Git Flow or trunk-based?

  • Git Flow: several versions in parallel (mobile apps, libraries, on-premise), with release/* branches to patch old versions.
  • Trunk-based: deploys many times a day, trusted tests, feature flags, branches that live a day or two.
  • GitHub Flow sits in between: short feature branches, PRs, CI, merge into main.

Choose by how you release, not by "one standard for the whole company".

3. In practice

Short branches, updated from main daily, history cleaned before the PR, force pushes that check first.

BASH
git switch -c feature/BK-123-coupon        # new branch from the latest main
git pull --rebase                          # update your branch, straight history
git rebase main                            # before opening the PR
# conflict: edit files, remove markers, then
git add src/Pricing.java && git rebase --continue
./mvnw -q test                             # always build and test after resolving
git rebase -i main                         # squash "wip", "fix typo"
git push --force-with-lease --force-if-includes
git revert 9f8e7d                          # undo on main: add an inverse commit
git reflog                                 # oops: find the hash, branch from it

--force-with-lease refuses if the remote changed since your last fetch; IDE background fetches defeat it, so add --force-if-includes. GitLab and Bitbucket have merge requests and branch protection just like GitHub.

Conflicts, reset and reflog: details worth remembering
  • Set git config merge.conflictStyle zdiff3: conflicts also show the merge base, so you see what each side changed from the original. git rerere remembers resolutions so repeated rebases don't make you resolve the same conflict again.
  • During a rebase, ours is the target branch (main) and theirs is your commit: the opposite of a merge.
  • reset --soft HEAD~1 drops the commit and keeps changes staged; --mixed (default) moves them to the working directory; --hard deletes them.
  • The reflog lives only on your machine and by default keeps reachable entries 90 days, unreachable ones 30 days. It doesn't recover uncommitted or unstaged changes.
  • git bisect run ./test.sh finds the commit that introduced a bug by halving; it needs every commit to build.

4. Working with AI

AI resolves conflicts quickly and confidently, but it sees lines, not the intent of each branch. Never let an agent run history-rewriting commands on a shared branch.

Hand to AI: explaining what each side changed relative to the merge base, a rebase -i plan, git bisect run scripts.

Check when AI resolves conflicts or cleans history:

  • git diff --check shows no markers; the intent of both branches is still there.
  • The build and the whole test suite rerun, not just tests for the conflicted file.
  • The agent doesn't run reset --hard, push --force or rebase on shared branches.
  • main and release/* have branch protection: no force pushes, PR and green CI required.

5. Exercises

Exercise 1: Review an AI-resolved conflict

main added tax, feature/coupon added a discount. The agent reports: "Resolved, kept both changes, build is green." Find the bug, name a test that proves it, fix it.

JAVA
<<<<<<< HEAD
    return b.nightlyPrice().multiply(BigDecimal.valueOf(b.nights())).multiply(VAT_RATE);
=======
    return b.nightlyPrice().multiply(BigDecimal.valueOf(b.nights())).subtract(b.discount());
>>>>>>> feature/coupon
// The agent's choice:
    return b.nightlyPrice().multiply(BigDecimal.valueOf(b.nights())).subtract(b.discount());
Solution to Exercise 1

The resolution drops the tax: the agent took the feature/coupon side wholesale but reported "kept both". The build is green because it compiles; the new tax test may not have run.

Test: 2 nights × 1,000,000, discount 100,000, VAT_RATE = 1.08: tax is expected, the agent's version returns 1,900,000.

Fix: combine both, but the order of tax and discount is a business decision, so ask whoever owns pricing first, e.g. (nightly × nights − discount) × VAT_RATE. Put the reason in the commit message.

Exercise 2: Read a vanished-commits incident

A colleague says 2 of their commits on release/2.4 disappeared. CI logs show the agent ran git push --force origin release/2.4 at 14:05. On the agent's machine:

$ git reflog show release/2.4
a11f0c2 release/2.4@{0}: reset: moving to HEAD~3
7be92d4 release/2.4@{1}: commit: fix(payment): retry IPN on 502
5c01e9a release/2.4@{2}: pull: Fast-forward

Diagnose it, say what you do in the first 15 minutes, and fix the root cause.

Solution to Exercise 2
  • Diagnosis: the agent ran reset --hard HEAD~3 then push --force (not --force-with-lease), overwriting the remote and dropping the commits after a11f0c2.
  • 15 minutes: tell the team to stop pushing to release/2.4; git branch rescue 7be92d4, check git log a11f0c2..rescue has every commit, then whoever holds the rights pushes it back. Commits missing from this machine's reflog come from the colleague's machine or the PR.
  • Root fix: branch protection forbids force pushes to main and release/*; agents can't push shared branches; shared branches are undone only with revert.

Summary

  • Short branches, updated from main daily.
  • Rebase only your own branches; push again with --force-with-lease.
  • After resolving conflicts, build and run the full test suite.
  • Shared branches use revert; the reflog recovers lost commits.
  • Choose Git Flow or trunk-based by how you release.

Read more: Part 1: Commits and the object model.

Practice interview questions on this topic

All DevOps & Cloud questions →

Loading comments…
Next on the roadmapBasic algorithms: Big-O and the three O(n²) sorts