Learn GitHub
$ gh ghcollaboration.ghbranches

Branches and GitHub Flow

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

$objectives

Branches and GitHub Flow

Collaboration

Module 2 of 5 · lesson 4 in this module

What you'll be able to do

  • Explain what a branch actually is and why it is cheap in Git
  • Create, switch, list, rename and delete branches safely
  • Merge a finished branch back into main
  • Tell a fast-forward merge from a true merge commit
  • Resolve a merge conflict calmly, marker by marker
  • Run the GitHub Flow loop from branch to merge
$concept

The idea, in plain language

A branch is a movable pointer to one commit. That is the entire definition. When you create a branch Git writes a new name in .git/refs/heads/ pointing at the commit you are on — a few dozen bytes, not a copy of your files. When you commit, only the branch you are on moves forward; every other branch keeps pointing exactly where it was. This is why Git users create branches constantly, and why the old advice about branching being expensive belongs to tools from another era.

main is a branch like any other, but by convention it holds the version that is deployed and working. That convention is what makes branching useful: if experimental work lives on its own branch, main can stay green, a broken experiment cannot break anyone else's checkout, and a half-finished feature never blocks a hotfix. The rule of thumb is short-lived and single-purpose — one branch per feature, bug fix or experiment, merged within days, then deleted.

GitHub Flow is the workflow built on that rule, in eight steps: branch off main, commit in small pieces, push the branch, open a pull request, let automated checks run, get a review, merge, delete the branch. Two things make it work. First, main is always deployable, so merging is the only risky moment and it is kept small. Second, integration happens through a pull request, so it is reviewed before it lands rather than after. When two branches touch the same lines, Git raises a merge conflict; the fix is always the same shape — open the file, choose what the final content should be, delete the markers, stage, commit.

$key terms

Key terms

Branch
A movable pointer to a commit; the series of commits reachable from it is that branch’s history.
main
The conventional default branch — always deployable, protected, and the target of merges.
HEAD
The pointer to whatever branch or commit you currently have checked out.
git switch
The modern command for changing branches; git checkout does the same plus more.
Fast-forward merge
The target branch simply slides forward to your commit — no merge commit, perfectly linear history.
Merge commit
A commit with two parents, created when the histories diverged and had to be joined.
Merge conflict
Git cannot decide between two versions of the same lines and asks you to choose.
GitHub Flow
The lightweight branch-per-change workflow: branch, commit, push, pull request, review, merge, delete.
Remote-tracking branch
origin/feature-x — your local record of a branch on the remote.
Detached HEAD
HEAD points at a commit rather than a branch; commits made there are easy to lose.
$steps

How it works, step by step

  1. 1
    Start from an up-to-date main

    Branch off the newest main, otherwise you inherit an old base and your pull request will look like it changes things it did not.

    sync firstbash
    git switch main
    git pull
  2. 2
    Create and switch to a branch in one step

    Name it by type and intent so anyone can read it: feature/, fix/, chore/, docs/, followed by a short slug.

    branch + switchbash
    git switch -c feature/login
  3. 3
    Commit in small, coherent steps

    Several small commits on the branch are easier to review and easier to squash than one enormous one. Push early — a pushed branch is a backup and a place to get early feedback.

    commit and publish the branchbash
    git add login.js
    git commit -m "Add login form"
    git push -u origin feature/login
  4. 4
    Merge back into main

    Locally you can merge your branch into main to test it; on GitHub the pull request does this for you. Either way the branch should be deleted once it is merged.

    merge and tidy upbash
    git switch main
    git pull
    git merge feature/login
    git branch -d feature/login
  5. 5
    Resolve a conflict when Git stops

    git status names the conflicted files. Open each one, keep the content you want, delete the <<<<<<< ======= >>>>>>> markers, then stage and commit. Aborting is always available with git merge --abort.

    conflict resolutionbash
    git status
    # both modified: login.js
    # ... edit the file, choose the final content ...
    git add login.js
    git commit
  6. 6
    Keep the branch current

    A long-lived branch drifts away from main and conflicts get worse. Pull main into the branch (or rebase onto it) regularly, not just before merging.

    stay currentbash
    git switch feature/login
    git fetch origin
    git merge origin/main
$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.

list branchesbash
git branch --all --verbose
Output
* main                3f9a1c2 Add deployment notes
  feature/login     8c1d2ab Add login form
  remotes/origin/main  3f9a1c2 Add deployment notes

The asterisk marks the current branch; --all includes remote-tracking branches.

create and switchbash
git switch -c feature/login
Output
Switched to a new branch 'feature/login'

Creates the branch at the current commit and moves HEAD onto it.

switch branchesbash
git switch main
Output
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

Refuses if you have uncommitted changes that would be overwritten — commit or stash them first.

renamebash
git branch -m feature/login feature/auth

Renames locally. If the branch is already pushed, the remote copy keeps the old name until you delete it.

mergebash
git merge feature/login
Output
Updating 3f9a1c2..8c1d2ab
Fast-forward
 login.js | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Fast-forward means main had no new commits, so the pointer simply moved. A merge commit appears when both sides moved.

delete a merged branchbash
git branch -d feature/login

The lowercase -d refuses to delete unmerged work. -D forces it, and that force is how branches get silently lost.

abort a messy mergebash
git merge --abort

Puts everything back exactly as it was before the merge started. Always available while a merge is in progress.

see what is mergedbash
git branch --merged
Output
  feature/login
* main

Lists branches already contained in the current one — the safe candidates for deletion.

$diagrams

Workflow diagrams

Diagram 1GitHub Flow, end to end

Two properties keep this safe: main never receives unreviewed work, and every branch is short-lived.

Reading the diagram
  1. Branch from the newest main so the change starts on a known-good base.
  2. Commit in small pieces; push early so the work is visible and backed up.
  3. Open a pull request: it is the review, the discussion and the merge button in one place.
  4. Automated checks and reviewers examine the change. If changes are requested, commit more on the same branch — the pull request updates itself.
  5. Merge once approved, then delete the branch. main is immediately deployable again.
Diagram 2Fast-forward versus merge commit

Same command, two shapes of history — and the shape tells you whether main moved while you worked.

Reading the diagram
  1. Fast-forward: the branch pointer moves and no new commit is created.
  2. Merge commit: both histories have moved, so Git records a commit whose two parents are the two branch tips.
  3. Conflicts: Git stops mid-merge and asks you to decide; git merge --abort undoes everything.
Diagram 3Anatomy of a conflict marker

The markers are just text around the two candidate versions of one region. You decide what stays and delete the markers.

Reading the diagram
  1. Everything between <<<<<<< HEAD and ======= is the version from the branch you are on.
  2. Everything between ======= and >>>>>>> is the version arriving from the other branch.
  3. Decide the final content — either version, both, or something new — delete all three marker lines, then git add the file and commit.
$example

Worked example

A complete branch-and-merge cycle with a real conflict resolved in the middle.

branch, conflict, mergebash
git switch -c feature/greeting
printf 'greeting = "Hello from the feature branch"\n' > config.py
git add config.py
git commit -m "Add greeting setting"

# meanwhile main changed the same line
git switch main
printf 'greeting = "Hello from main"\n' > config.py
git commit -am "Add greeting setting to main"

git merge feature/greeting
# Auto-merging config.py
# CONFLICT (content): Merge conflict in config.py
# Automatic merge failed; fix conflicts and then commit the result.

cat config.py
# <<<<<<< HEAD
# greeting = "Hello from main"
# =======
# greeting = "Hello from the feature branch"
# >>>>>>> feature/greeting

printf 'greeting = "Hello from the feature branch"\n' > config.py
git add config.py
git commit -m "Merge feature/greeting; keep feature wording"
git branch -d feature/greeting
Output
One conflict, one deliberate decision, one merge commit. main now contains the wording you chose.
1The feature branch is created from main and one file is changed on it.
2Back on main, the same line is changed differently. This is what makes the conflict inevitable — two branches editing the same lines since they diverged.
3git merge reports CONFLICT and, importantly, says what to do next: "fix conflicts and then commit the result". The merge is paused, not broken.
4cat shows the markers. HEAD is the branch you were on (main), and the section below ======= is the incoming feature branch.
5We choose the feature wording, overwrite the file cleanly so no markers remain, and stage it. Staging is how you tell Git the conflict is resolved.
6The commit completes the merge. Deleting the branch afterwards keeps git branch output readable.
$use cases

Where you'll use it

▸Building a feature over several days while main stays deployable.
▸Fixing a production bug on a hotfix branch without shipping unrelated work in progress.
▸Letting two developers work on the same area of code and reconciling through a review.
▸Prototyping an idea you may never merge — the branch costs almost nothing.
▸Keeping a documentation branch separate from code changes so reviews stay focused.
▸Tagging a release branch when maintaining two supported versions at once.
$mistakes

Common mistakes

  • Doing every change directly on main, so a broken experiment blocks everyone.
  • Letting a feature branch live for weeks, then facing a conflict in every file it touches.
  • Deleting a branch with -D without checking git branch --merged first.
  • Committing while in a detached HEAD state and losing the commits when switching away.
  • Deleting the conflict markers by hand but forgetting to git add the file, so the merge never completes.
  • Branches named "test", "stuff" or "john2" — nobody can tell what they contain.
$best practices

Best practices

  • Branch from an up-to-date main, every time.
  • Keep branches short-lived and single-purpose; merge within days, not weeks.
  • Use a naming convention such as feature/, fix/, chore/, docs/ plus a short slug.
  • Delete the branch as soon as it is merged — the pull request keeps the history.
  • Before a big merge, create a throwaway copy of the branch so you can experiment with conflict resolution safely.
  • When a conflict looks large, run git merge --abort and re-plan instead of fighting it.
$exercise

Hands-on exercise

Cause a conflict on purpose, then resolve it

Conflicts are only frightening until you have made one deliberately.

  1. In a practice repository, create branch feature/alpha, add a file with three lines of text, and commit it.
  2. Switch back to main and add the same file with different content on the same lines, then commit.
  3. Merge feature/alpha into main and read the CONFLICT output carefully — note which file and which lines are named.
  4. Open the file and identify which side is HEAD and which side is the incoming branch. Write down the answer before changing anything.
  5. Resolve the file so a third version of the lines exists — neither side exactly — then stage and commit it.
  6. Run git log --oneline --graph to see the merge commit, then delete the branch and confirm git branch no longer lists it.

💡 Hint: git status during a conflict lists files under "Unmerged paths". If you want a clean restart at any point, git merge --abort returns the repository to its pre-merge state.

Done when: git log --graph shows a merge commit with two parents, the resolved file contains no conflict markers, and the feature branch is gone.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    What does creating a branch in Git actually copy?

  2. Command outputQ2

    What does this merge output tell you?

    Updating 3f9a1c2..8c1d2ab
    Fast-forward
  3. TroubleshootingQ3

    You resolved a conflict, saved the file, and ran git commit — but Git says the merge is still in progress and the file is still unmerged. What did you forget?

  4. True or falseQ4

    True or false: git branch -d refuses to delete a branch whose commits are not merged.

  5. Multiple choiceQ5

    Why does GitHub Flow insist that main always stays deployable?

$summary

Module summary

  • A branch is a movable pointer to a commit — cheap because it copies nothing.
  • main stays deployable; every change gets its own short-lived, single-purpose branch.
  • git switch -c creates and switches; git branch -d deletes safely; git branch --merged tells you what is safe to remove.
  • A fast-forward slides the pointer forward; a merge commit joins two histories and has two parents.
  • Conflicts are Git asking a question: keep yours, theirs or both — then delete the markers, stage and commit.
  • GitHub Flow is branch, commit, push, pull request, checks, review, merge, delete.

Next up: Branches are only half of the workflow. Module 5 covers the pull request itself: how to describe a change so a reviewer can approve it quickly, and how to respond when they ask for edits.

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.