Advanced Branching and Git Workflows
6 learning objectives · 11 key terms · 8 commands · 3 diagrams · 5 quiz questions
Advanced Branching and Git Workflows
Advanced GitWhat you'll be able to do
- Compare feature branch, trunk-based, Git Flow and release workflows
- Rebase a branch onto main and explain when rebasing is a bad idea
- Squash a messy series of commits with interactive rebase
- Choose correctly between merge, rebase, squash merge and cherry-pick
- Tag releases and apply semantic versioning meaningfully
- Keep long-running branches and release lines synchronised
The idea, in plain language
Every branching workflow is an answer to the same question: how much unreviewed work is allowed to exist at once, and how does it get back into the deployable branch? Trunk-based development says almost none — small commits land on main constantly, behind feature flags, and the deploy pipeline decides when they are live. Git Flow says a lot: long-lived develop, feature, release and hotfix branches, which suits products with scheduled, versioned releases and multiple supported versions. GitHub Flow, from Module 4, sits between them and is the default for continuously deployed web software. None of these is "more advanced" than the others; each trades integration risk against release-planning ceremony.
Rebasing is the tool that keeps trunk-based and feature-branch workflows readable. Instead of merging main into your branch (which adds a merge commit every time), rebase replays your commits on top of the newest main, producing a linear history where your branch appears to have been written just now. The price is that the commits are rewritten — they get new hashes — which is why the rule is: rebase your own unpublished branch, never a branch that other people have pulled from. If you have already pushed the branch, use --force-with-lease after the rebase, and never on main.
Tags and releases are how history becomes something you can point at. A tag is a permanent label on a commit; semantic versioning gives that label meaning (MAJOR for breaking changes, MINOR for compatible features, PATCH for compatible fixes); and a GitHub release attaches human-readable notes and downloadable artefacts to the tag. When maintaining more than one version, release branches are where fixes land first and then flow back to main, usually as a cherry-pick so the fix arrives as a clean single commit.
Key terms
- Trunk-based development
- Everyone commits to main frequently; long-lived branches are avoided.
- Feature branch workflow
- One branch per feature, merged via pull request — the GitHub Flow default.
- Git Flow
- A structured model with develop, feature, release and hotfix branches for scheduled releases.
- Release branch
- A branch frozen for a version, receiving only stabilisation fixes.
- Hotfix branch
- A branch off a release or tag carrying an urgent production fix.
- Rebase
- Replaying your commits onto a new base — linear history, rewritten hashes.
- Interactive rebase
- git rebase -i: reorder, squash, reword, edit or drop commits in a series.
- Squash commit
- Several commits combined into one, either locally or by GitHub on merge.
- Tag
- A permanent name for a specific commit, usually an annotated release marker.
- Semantic versioning
- MAJOR.MINOR.PATCH — the meaning of each number is a contract with your users.
- Feature flag
- A runtime switch that lets unfinished code ship on main without being visible.
How it works, step by step
- 1Pick the workflow before you need it
Continuously deployed web app: GitHub Flow. Versioned library or app with scheduled releases: release branches plus hotfixes. Large monorepo with many teams: trunk-based with feature flags. Publishing the choice prevents the usual argument.
- 2Sync your branch by rebasing onto main
Fetch first, then rebase. Your commits are replayed on top of the newest main, so the eventual merge is a fast-forward and history stays linear.
rebase your own branchbashgit fetch origin git rebase origin/main - 3Resolve any conflict as it appears, commit by commit
Rebase stops at the first conflicting commit. Fix it, git add the file, then git rebase --continue. To abandon the rebase entirely, git rebase --abort.
during a rebasebashgit add resolved-file.js git rebase --continue # or, to back out completely git rebase --abort
- 4Clean up the series with interactive rebase
Before opening a pull request, squash the "fix typo" commits into the commits they belong to. Rewords and squashes produce a series a reviewer can read in order.
tidy the seriesbashgit rebase -i HEAD~5 # pick 4f1c2ab Add login form # squash 8c1d2ab fix label # reword 9a7b6c5 Add validation # drop 1f2e3d4 debug logging
- 5Push your rewrite safely
Because the hashes changed, a normal push is rejected. --force-with-lease only overwrites if nobody else pushed to that branch since your last fetch.
never on mainbashgit push --force-with-lease origin feature/login
- 6Tag and release the result
Annotated tags carry a message and are the right choice for releases. Push tags explicitly, then create the release on GitHub so notes and artefacts attach to it.
releasebashgit tag -a v2.5.0 -m "v2.5.0 — session handling improvements" git push origin v2.5.0 gh release create v2.5.0 --generate-notes
- 7Keep long-running branches honest
Release branches receive fixes only; those fixes must reach main too, or the next release regresses them. Cherry-pick the fix into main, or merge main forward regularly.
forward-port fixesbashgit switch main git cherry-pick 8c1d2ab
Commands worth memorising
Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.
git rebase origin/mainSuccessfully rebased and updated refs/heads/feature/login.
Rewrites your commits with new hashes on top of the newest main. Only do this on a branch nobody else is using.
git rebase -i HEAD~4
Opens your editor with one line per commit. Change pick to squash, reword, edit or drop; the order of the lines is the order of the commits.
git rebase --abortEverything is restored exactly as it was before the rebase began. Always available while a rebase is in progress.
git push --force-with-lease origin feature/login
+ 8c1d2ab...1f2e3d4 feature/login -> feature/login (forced update)
Succeeds only if the remote branch is where you last saw it, so a colleague’s push is never silently overwritten.
git tag -a v2.5.0 -m "v2.5.0 — session handling improvements"
Annotated tags store an author, date and message — use them for releases; lightweight tags are for local bookmarks.
git push origin --tags* [new tag] v2.5.0 -> v2.5.0
Tags are not pushed by git push alone; they need their own push, since they are not branches.
gh release create v2.5.0 --generate-notes
https://github.com/ada/app/releases/tag/v2.5.0
--generate-notes lists merged pull requests since the previous release, which is usually 80% of the changelog.
git switch -c hotfix/2.5.1 v2.5.0 # ... fix and commit ... git tag -a v2.5.1 -m "v2.5.1 — fix null pointer in export"
Branch from the tag, not from main, so the fix contains only what that release needs.
Workflow diagrams
Rebase keeps the branch current, squash keeps main readable, and the release is a tag on a reviewed commit.
- Work starts from an issue so the intent is recorded before the code exists.
- Commits accumulate on a short-lived branch and are rebased onto main whenever main moves.
- The pull request triggers CI; failures are fixed by pushing more commits (and rebasing again if needed).
- Once approved and green, squash-merge keeps main history readable.
- At release time an annotated tag is pushed and a GitHub release attaches notes to it.
| Operation | What it does to history | Safe on shared branches? | Best used for |
|---|---|---|---|
| git merge | Adds a merge commit with two parents | Yes — only adds commits | Integrating a finished branch, keeping its individual commits |
| git rebase | Replays commits on a new base; new hashes | No — rewrites history, requires force-push | Updating your own unpublished branch onto main |
| Squash merge (GitHub) | One new commit containing the whole branch | Yes — main only gains a commit | Most feature pull requests |
| git cherry-pick | Copies one commit as a new commit on the current branch | Yes — only adds a commit | Forward-porting a hotfix, back-porting to a release line |
Four operations, four different effects on history. Pick by what you want the log to look like, not by habit.
- Merge and squash add history, so they are safe anywhere.
- Rebase rewrites, so it belongs only on branches nobody else has pulled.
- Cherry-pick is the surgical option: one commit, one place, with no branch relationship implied.
| Workflow | Branches | Release cadence | Fits |
|---|---|---|---|
| Trunk-based | main plus very short-lived branches | Continuous, many per day | Web apps with strong automated tests and feature flags |
| GitHub Flow | main plus one branch per change | Continuous or daily | Most teams; the default on GitHub |
| Git Flow | develop, feature, release, hotfix, main | Scheduled releases | Versioned products supporting multiple releases |
| Release branches | main plus one per supported version | Per version, hotfixes between | Libraries and software with upkeep windows |
The right answer depends on how often you release and how many versions you must support.
- More branches means more release ceremony and more integration risk; less branching demands more automated testing.
- Match the workflow to your deployment reality rather than to fashion.
Worked example
Tidying a messy branch and shipping a tagged release — the shape of a normal professional day.
git log --oneline # 1f2e3d4 debug logging # 9a7b6c5 Add validation # 8c1d2ab fix label typo # 4f1c2ab Add login form git rebase -i HEAD~4 # pick 4f1c2ab Add login form # squash 8c1d2ab fix label typo # reword 9a7b6c5 Add validation # drop 1f2e3d4 debug logging git log --oneline # b7c8d9e Add validation # 3c4d5e6 Add login form git fetch origin git rebase origin/main # Successfully rebased and updated refs/heads/feature/login. git push --force-with-lease origin feature/login # + b7c8d9e...0a1b2c3 feature/login -> feature/login (forced update) # after the pull request is squash-merged git switch main && git pull git tag -a v2.5.0 -m "v2.5.0 — login and session improvements" git push origin v2.5.0 gh release create v2.5.0 --generate-notes
Two clean commits instead of four messy ones, a linear branch on top of newest main, and a tagged release with generated notes.
Where you'll use it
Common mistakes
- Rebasing a branch that colleagues have pulled, then force-pushing and rewriting their history.
- Force-pushing main — the single most destructive command in everyday Git use.
- Using git push --force instead of --force-with-lease and silently deleting a teammate’s commits.
- Interactive rebasing commits that have already been merged into main.
- Tagging releases without a message (lightweight tags) and losing the release context.
- Fixing a bug only on a release branch and never forward-porting it to main, so the next release regresses.
- Treating semantic version numbers as decoration: shipping a breaking change as a PATCH bump breaks every consumer.
Best practices
- Branch from main, rebase your own branch often, and merge through a pull request.
- Never rebase or force-push a branch that others have pulled — including main.
- Clean up the commit series with interactive rebase before requesting review.
- Use --force-with-lease, never bare --force.
- Tag releases with annotated tags and bump the version according to what actually changed.
- Keep release branches minimal: fixes only, and forward-port each one to main.
- Match branch names to issues so the workflow stays traceable from board to release.
Hands-on exercise
Rebase, squash and release a versioned change
Practise the whole professional flow on a practice repository.
- Create a branch and deliberately make four commits: two real changes, one typo fix and one debug logging line.
- On main, make an unrelated commit so the histories have diverged.
- Run git rebase -i HEAD~4 on your branch and produce exactly two clean commits with no debug line.
- Rebase the branch onto the newest main: git fetch origin, then git rebase origin/main.
- Push with --force-with-lease, open a pull request, squash-merge it, and pull main locally.
- Tag the merged result v0.1.0 with an annotated tag, push the tag, and create a GitHub release with generated notes. Explain in one sentence why this bump is MINOR and not MAJOR.
💡 Hint: If a rebase goes wrong mid-way, git rebase --abort restores the exact state you started from, including the original commits and hashes.
Done when: Your branch contains two clean commits, main is linear, and the repository has an annotated v0.1.0 tag with a release attached.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
When must you avoid git rebase?
- Command outputQ2
What does this output indicate?
+ b7c8d9e...0a1b2c3 feature/login -> feature/login (forced update)
- TroubleshootingQ3
A production bug was fixed on the release/v2.5 branch. The next release from main still contains the bug. Which command fixes this correctly?
- Multiple choiceQ4
Using semantic versioning, which change requires a MAJOR version bump?
- True or falseQ5
True or false: a squash merge keeps every commit from the feature branch in main’s history.
Module summary
- Workflow choice is a trade between integration risk and release ceremony: trunk-based, GitHub Flow, Git Flow, release branches.
- Rebase your own unpublished branch to keep history linear; never rebase shared branches.
- Interactive rebase tidies a series with squash, reword and drop before review.
- Merge and squash add history; rebase rewrites; cherry-pick copies one commit. Only rebase is unsafe on shared branches.
- After a rebase, push with --force-with-lease and never to main.
- Annotated tags plus semantic versioning plus a GitHub release turn a commit into something users can depend on.
- Fixes made on a release branch must be forward-ported to main.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.