Branches and GitHub Flow
6 learning objectives · 10 key terms · 8 commands · 3 diagrams · 5 quiz questions
Branches and GitHub Flow
CollaborationWhat 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
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
- 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.
How it works, step by step
- 1Start 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 firstbashgit switch main git pull - 2Create 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 + switchbashgit switch -c feature/login
- 3Commit 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 branchbashgit add login.js git commit -m "Add login form" git push -u origin feature/login
- 4Merge 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 upbashgit switch main git pull git merge feature/login git branch -d feature/login
- 5Resolve 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 resolutionbashgit status # both modified: login.js # ... edit the file, choose the final content ... git add login.js git commit
- 6Keep 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 currentbashgit switch feature/login git fetch origin git merge origin/main
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 branch --all --verbose
* 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.
git switch -c feature/login
Switched to a new branch 'feature/login'
Creates the branch at the current commit and moves HEAD onto it.
git switch mainSwitched 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.
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.
git merge feature/loginUpdating 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.
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.
git merge --abortPuts everything back exactly as it was before the merge started. Always available while a merge is in progress.
git branch --mergedfeature/login * main
Lists branches already contained in the current one — the safe candidates for deletion.
Workflow diagrams
Two properties keep this safe: main never receives unreviewed work, and every branch is short-lived.
- Branch from the newest main so the change starts on a known-good base.
- Commit in small pieces; push early so the work is visible and backed up.
- Open a pull request: it is the review, the discussion and the merge button in one place.
- Automated checks and reviewers examine the change. If changes are requested, commit more on the same branch — the pull request updates itself.
- Merge once approved, then delete the branch. main is immediately deployable again.
| Situation | What git merge does | Resulting history |
|---|---|---|
| main has not moved since you branched | Slides main forward to your commit (fast-forward) | Perfectly linear, no extra commit |
| main gained commits while you worked | Creates a merge commit with two parents | A visible join point; history branches and rejoins |
| You rebase instead of merging | Replays your commits on top of main | Linear, but your commit hashes change |
| Git cannot pick a winner | Stops and marks conflicts in the files | You resolve, stage and commit to finish the merge |
Same command, two shapes of history — and the shape tells you whether main moved while you worked.
- Fast-forward: the branch pointer moves and no new commit is created.
- Merge commit: both histories have moved, so Git records a commit whose two parents are the two branch tips.
- Conflicts: Git stops mid-merge and asks you to decide; git merge --abort undoes everything.
The markers are just text around the two candidate versions of one region. You decide what stays and delete the markers.
- Everything between <<<<<<< HEAD and ======= is the version from the branch you are on.
- Everything between ======= and >>>>>>> is the version arriving from the other branch.
- Decide the final content — either version, both, or something new — delete all three marker lines, then git add the file and commit.
Worked example
A complete branch-and-merge cycle with a real conflict resolved in the middle.
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
One conflict, one deliberate decision, one merge commit. main now contains the wording you chose.
Where you'll use it
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
- 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.
Hands-on exercise
Cause a conflict on purpose, then resolve it
Conflicts are only frightening until you have made one deliberately.
- In a practice repository, create branch feature/alpha, add a file with three lines of text, and commit it.
- Switch back to main and add the same file with different content on the same lines, then commit.
- Merge feature/alpha into main and read the CONFLICT output carefully — note which file and which lines are named.
- Open the file and identify which side is HEAD and which side is the incoming branch. Write down the answer before changing anything.
- Resolve the file so a third version of the lines exists — neither side exactly — then stage and commit it.
- 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.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
What does creating a branch in Git actually copy?
- Command outputQ2
What does this merge output tell you?
Updating 3f9a1c2..8c1d2ab Fast-forward
- 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?
- True or falseQ4
True or false: git branch -d refuses to delete a branch whose commits are not merged.
- Multiple choiceQ5
Why does GitHub Flow insist that main always stays deployable?
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.
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.