Learn GitHub
$ gh ghadvancedgit.ghadvancedbranching

Advanced Branching and Git Workflows

6 learning objectives · 11 key terms · 8 commands · 3 diagrams · 5 quiz questions

$objectives

Advanced Branching and Git Workflows

Advanced Git

Module 3 of 5 · lesson 9 in this module

What 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
$concept

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

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.
$steps

How it works, step by step

  1. 1
    Pick 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.

  2. 2
    Sync 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 branchbash
    git fetch origin
    git rebase origin/main
  3. 3
    Resolve 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 rebasebash
    git add resolved-file.js
    git rebase --continue
    # or, to back out completely
    git rebase --abort
  4. 4
    Clean 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 seriesbash
    git rebase -i HEAD~5
    # pick  4f1c2ab Add login form
    # squash 8c1d2ab fix label
    # reword 9a7b6c5 Add validation
    # drop  1f2e3d4 debug logging
  5. 5
    Push 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 mainbash
    git push --force-with-lease origin feature/login
  6. 6
    Tag 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.

    releasebash
    git 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
  7. 7
    Keep 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 fixesbash
    git switch main
    git cherry-pick 8c1d2ab
$commands

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.

rebase onto mainbash
git rebase origin/main
Output
Successfully 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.

interactive rebasebash
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.

abort a rebasebash
git rebase --abort

Everything is restored exactly as it was before the rebase began. Always available while a rebase is in progress.

safe force-pushbash
git push --force-with-lease origin feature/login
Output
+ 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.

annotated tagbash
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.

list and push tagsbash
git push origin --tags
Output
 * [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.

create a releasebash
gh release create v2.5.0 --generate-notes
Output
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.

hotfix a released versionbash
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.

$diagrams

Workflow diagrams

Diagram 1The professional feature flow

Rebase keeps the branch current, squash keeps main readable, and the release is a tag on a reviewed commit.

Reading the diagram
  1. Work starts from an issue so the intent is recorded before the code exists.
  2. Commits accumulate on a short-lived branch and are rebased onto main whenever main moves.
  3. The pull request triggers CI; failures are fixed by pushing more commits (and rebasing again if needed).
  4. Once approved and green, squash-merge keeps main history readable.
  5. At release time an annotated tag is pushed and a GitHub release attaches notes to it.
Diagram 2Merge, rebase, squash merge and cherry-pick

Four operations, four different effects on history. Pick by what you want the log to look like, not by habit.

Reading the diagram
  1. Merge and squash add history, so they are safe anywhere.
  2. Rebase rewrites, so it belongs only on branches nobody else has pulled.
  3. Cherry-pick is the surgical option: one commit, one place, with no branch relationship implied.
Diagram 3Which workflow fits your team?

The right answer depends on how often you release and how many versions you must support.

Reading the diagram
  1. More branches means more release ceremony and more integration risk; less branching demands more automated testing.
  2. Match the workflow to your deployment reality rather than to fashion.
$example

Worked example

Tidying a messy branch and shipping a tagged release — the shape of a normal professional day.

clean up, rebase, releasebash
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
Output
Two clean commits instead of four messy ones, a linear branch on top of newest main, and a tagged release with generated notes.
1The starting log shows the classic mess: two good commits, one typo fix and one debug line that must not ship.
2The interactive rebase plan reads top to bottom: keep the first commit, squash the typo fix into it, reword the validation commit, and drop the debug commit entirely.
3The resulting log has no typo commit and no debug commit — the reviewer sees two intentional commits.
4Rebasing onto origin/main replays those two commits on the newest main, so the pull request diff contains only your work and the merge will be trivial.
5The rebase changed the commit hashes, so the push needs --force-with-lease. It succeeds because nobody else pushed to this branch in the meantime.
6After the squash-merge, main is pulled, an annotated tag marks the release commit, and gh release create attaches notes generated from merged pull requests.
$use cases

Where you'll use it

▸Keeping a feature branch current with a busy main branch without a trail of merge commits.
▸Turning a week of messy commits into two reviewable ones before opening a pull request.
▸Releasing v2.5.0 with notes and artefacts attached to a tag.
▸Forward-porting a production hotfix from a release branch into main.
▸Running trunk-based development behind feature flags while tests protect main.
▸Supporting two released versions with separate release branches.
$mistakes

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

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.
$exercise

Hands-on exercise

Rebase, squash and release a versioned change

Practise the whole professional flow on a practice repository.

  1. Create a branch and deliberately make four commits: two real changes, one typo fix and one debug logging line.
  2. On main, make an unrelated commit so the histories have diverged.
  3. Run git rebase -i HEAD~4 on your branch and produce exactly two clean commits with no debug line.
  4. Rebase the branch onto the newest main: git fetch origin, then git rebase origin/main.
  5. Push with --force-with-lease, open a pull request, squash-merge it, and pull main locally.
  6. 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.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    When must you avoid git rebase?

  2. Command outputQ2

    What does this output indicate?

    + b7c8d9e...0a1b2c3 feature/login -> feature/login (forced update)
  3. 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?

  4. Multiple choiceQ4

    Using semantic versioning, which change requires a MAJOR version bump?

  5. True or falseQ5

    True or false: a squash merge keeps every commit from the feature branch in main’s history.

$summary

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.

Next up: Everything so far is human-driven. Module 10 makes the machine do the checking: GitHub Actions runs your tests, linters and deployments on every push.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

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.